Results 1 to 3 of 3
  1. #1
    Lewis is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2012
    Posts
    7

    Basic query to vba translations

    Hi guys,

    I don't have a lot of experience with vba but enough to make sense of the code. I'm trying to find simple examples illustrating the vba equivalent of the various query types. For example, the following performs an append to a table based on text box's on a form...
    Code:
    Dim db As database
    Dim rec As Recordset
    
    Set db = CurrentDb
    Set rec = db.OpenRecordset("Select * from tblTable")
    
    rec.AddNew
    rec("tblField1") = Me.[Text1]
    rec("tblField2") = Me.[Text2]
    rec update
    
    Set rec = Nothing
    Set db = Nothing
    So, it would seem that "update" = Append in this code, a strange translation, but ok, I can handle it.

    What if I want to use one of the text boxes as the criteria for a record in the table and use the other text box as an update value of another field in the table? Insert? Seems I've seen that before somewhere, but could be wrong. Still, a basic example would be helpful. Same for Select and Delete types if possible....or a resource/example?

    The old dog's learning new tricks, so any help is appreciated.



    Bos

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Concatenate variables. Reference to control on form is a variable.

    "SELECT * FROM tblTable WHERE ID=" & Me.Text1

    "DELETE FROM tblTABLE WHERE ID=" & Me.Text1

    "UPDATE tblTABLE SET Field1=" & Me.Text1 & " WHERE ID=" & Me.ID

    "INSERT INTO tblTable(Field1, Field2, Field3) VALUES(" & Me.Text1 & ", '" & Me.Text2 & "', #" & Me.Text3 & "#")

    Can use Execute method in VBA for SQL actions:

    CurrentDb.Execute "INSERT INTO tblTable(Field1, Field2, Field3) VALUES(" & Me.Text1 & ", '" & Me.Text2 & "', #" & Me.Text3 & "#")
    Last edited by June7; 05-08-2015 at 08:27 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Use queries, not VB.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Translations
    By shubrook in forum Queries
    Replies: 1
    Last Post: 05-02-2011, 04:13 PM
  2. Query Help VERY BASIC
    By nparrillo in forum Queries
    Replies: 5
    Last Post: 04-06-2011, 11:15 AM
  3. Basic Query/totaling fields in a query
    By afisher in forum Queries
    Replies: 6
    Last Post: 08-05-2010, 01:43 PM
  4. Query in visual basic
    By Lucas83 in forum Programming
    Replies: 1
    Last Post: 06-10-2010, 11:00 AM
  5. basic query codes
    By joms222 in forum Queries
    Replies: 1
    Last Post: 03-20-2009, 11:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums