Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 28th, 2007, 10:07 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Variable Usage in Insert Command

I am trying to call a variable into my Insert Statement

name = mem_first_name
strSQL = "insert into Test ( FirstName ) Values (" & name & ")"


Test is a table in an Access database. If I type the actual value then it works, but doesn't work with a variable

 
Old July 29th, 2007, 02:42 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Since "name" is a String, then it will need to be encased in Single Quotes ' '.

Try:
name = "'" & mem_first_name & "'"
strSQL = "insert into Test ( FirstName ) Values (" & name & ")"

Hope this helps,
Rob

<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>
 
Old July 29th, 2007, 08:52 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rob, it doesn't work. It gives me a syntax error.

 
Old July 30th, 2007, 02:53 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Tara,

Please try and be more descriptive with your problems.
What like is causing the error? Is it within the code, or when you try and execute the query?

Oh, and apologies, I should have said in my first post, welcome to the forum!

<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>
 
Old July 30th, 2007, 07:09 AM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!!!

I am coding in Extra (mainframe) application. I am doing a screen scrape and then appending the data into an Access table. Some of the VB commands work and some aren't. Below is the code where I am screen scraping and then trying to append this into Access:

mem_first_name = Sess0.Screen.GetString(15, 09, 12)

Const DBNAME = "C:\Work.mdb"
   Dim connection_string As String, cn As Object, cmd As Object connection_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBNAME & ";Persist Security Info=False"

   Set cn = CreateObject("ADODB.Connection")
   Set cmd = CreateObject("ADODB.Command")

   cn.ConnectionString = connection_string
   cn.Open
   cmd.ActiveConnection = cn

   name = "'" & mem_first_name & "'"
   strSQL = "insert into Test ( FirstName ) Values (" & name & ")"
   cmd.CommandText = strSQL
   cmd.Execute

   Set cmd = Nothing
   Set cn = Nothing

End Sub
*------------------------------------*
It gives me a syntax error at the name statement.






 
Old July 30th, 2007, 09:56 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Looks like your running VBScript yes.. ?

I dont see a declaration for the name variable?
I don't know if not declaring a variable throws a syntax error
(been a long time since I did VBScript!)

Hope this helps,
Rob

<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>
 
Old July 30th, 2007, 12:25 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for help!!!

But this is what worked:


StrSQL = "insert into Test(FirstName) Values ('" &Cstr(mem_first_name)& "')"

It is Cstr which made it work.

Thanks!!!

Tara

 
Old July 30th, 2007, 12:50 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Tara,

As I said, didn't know what "mem_first_name" was, or where you got it.
VBScript is a dynamic language, and while it brings benefits, its type-less
structure can make odd errors occur. Explicitly calling CStr forces the
scripting engine to treat it like a String.

Glad you got it working, please shout if you need anything else.

Regards,
Rob

<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Having Problems with my Insert Command - RESOLVED Andrew.Berry ASP.NET 2.0 Basics 1 October 2nd, 2007 09:59 AM
Insert Command using a querystring Andrew.Berry ASP.NET 2.0 Basics 1 September 27th, 2007 09:26 AM
Insert command rajanikrishna ASP.NET 1.0 and 1.1 Basics 0 April 3rd, 2006 08:47 AM
ASP Insert into command joshua_mccrea Classic ASP Databases 5 August 13th, 2003 10:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.