Click to See Complete Forum and Search --> : Running a Query in VB within Access


webraycer
07-14-2005, 11:44 AM
I need to run the following query in VB, but it goes in an event procedure for a form button in Access. It's looking up a language abbreviation given the language name.

SELECT abbreviation FROM languages WHERE name = Me.languageForm

Where languageForm contains the language name.

Thanks

Paladyr
07-14-2005, 03:56 PM
If you are talking about using an ADODB.Recordset object you would just do something like this:

Dim rstRS as New ADODB.Recordset

rstRS.Open "Put query here", CurrentProject.Connection, adOpenKeyset, adLockReadOnly

webraycer
07-15-2005, 04:15 PM
If you are talking about using an ADODB.Recordset object you would just do something like this:

Dim rstRS as New ADODB.Recordset

rstRS.Open "Put query here", CurrentProject.Connection, adOpenKeyset, adLockReadOnly
"No value given for one or more required parameters"
That's what I see in an alert window for the rstRS.Open line.

Also, is this syntax correct to read the results of a query:Me.filename = Me.filename & rstRS.Fields.Item(0)

Paladyr
07-15-2005, 06:22 PM
"No value given for one or more required parameters"
That's what I see in an alert window for the rstRS.Open line.

Also, is this syntax correct to read the results of a query:Me.filename = Me.filename & rstRS.Fields.Item(0)

That's not correct. You can read the results by either:

rstRS("Field_Name")

or

rstRS!Field_Name

if you have a space

rstRS![Field Name]

plus depending on what you are assigning the value to you may need to convert the field to a string, number, whatever...

Dim str1 as String

str1 = CStr(rstRS("Field_Name"))

webraycer
07-18-2005, 09:01 AM
Thanks -- but my rstRS.Open line is still broken with the same error.

webraycer
07-18-2005, 09:44 AM
It works now -- I forgot to put single quotes around a value in the query -- I had "WHERE name=targetname" instead of "WHERE name='targetname'"

Why that gave me the error I got I have no idea -- most programming languages give a query syntax error.

Paladyr
07-18-2005, 05:24 PM
It works now -- I forgot to put single quotes around a value in the query -- I had "WHERE name=targetname" instead of "WHERE name='targetname'"

Why that gave me the error I got I have no idea -- most programming languages give a query syntax error.

when you are opening recordsets, you will only get errors that the recordset can generate, rather than an error about interpreting the SQL statement. Glad it works!