Running a Query in VB within Access

Sharky Forums


Results 1 to 7 of 7

Thread: Running a Query in VB within Access

  1. #1
    Catfish webraycer's Avatar
    Join Date
    Dec 2001
    Location
    Midlothian, VA, USA
    Posts
    187

    Running a Query in VB within Access [solved]

    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.
    Code:
    SELECT abbreviation FROM languages WHERE name = Me.languageForm
    Where languageForm contains the language name.

    Thanks
    Last edited by webraycer; 07-18-2005 at 09:45 AM.

  2. #2
    Mako Shark Paladyr's Avatar
    Join Date
    Mar 2001
    Location
    Cinci, OH
    Posts
    4,562
    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
    Core 2 Duo w Radeon 4850, Droid Phone, Mits HD1000U projector

  3. #3
    Catfish webraycer's Avatar
    Join Date
    Dec 2001
    Location
    Midlothian, VA, USA
    Posts
    187
    Quote Originally Posted by Paladyr
    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:
    Code:
    Me.filename = Me.filename & rstRS.Fields.Item(0)

  4. #4
    Mako Shark Paladyr's Avatar
    Join Date
    Mar 2001
    Location
    Cinci, OH
    Posts
    4,562
    Quote Originally Posted by webraycer
    "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:
    Code:
    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"))
    Core 2 Duo w Radeon 4850, Droid Phone, Mits HD1000U projector

  5. #5
    Catfish webraycer's Avatar
    Join Date
    Dec 2001
    Location
    Midlothian, VA, USA
    Posts
    187
    Thanks -- but my rstRS.Open line is still broken with the same error.

  6. #6
    Catfish webraycer's Avatar
    Join Date
    Dec 2001
    Location
    Midlothian, VA, USA
    Posts
    187
    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.

  7. #7
    Mako Shark Paladyr's Avatar
    Join Date
    Mar 2001
    Location
    Cinci, OH
    Posts
    4,562
    Quote Originally Posted by webraycer
    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!
    Core 2 Duo w Radeon 4850, Droid Phone, Mits HD1000U projector

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •