Results 1 to 7 of 7
  1. #1
    Duncan is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Oct 2010
    Posts
    71

    VBA search code

    Having a little trouble with some VBA code designed to grab a number from a form (MRN) then use this data to find the corresponding data in a table making a small change to that record (changing a checkbox to true). The table opens but only the first record is updated. The code works well as a standard VBA search code on another form. Any help great full.

    Dim SearchMRNList As Variant

    SearchMRNList = Me!MRN [name of textbox on form holding search data]

    myRSInfectionDC.Open "tblClientAssessmentCDInfection", , adOpenDynamic, adLockOptimistic

    DoCmd.GoToControl ("txtMRN") [name of column to be searched]
    DoCmd.FindRecord SearchMRNList

    With myRSInfectionDC
    !DC = True
    .Update
    End With

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Your code is only designed to update the first record it finds.
    You might consider including a recordset object and looping through the records in your table to update the rows which have matching data.

  3. #3
    Duncan is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Oct 2010
    Posts
    71
    Thanks for the reply. You are correct so I have spent the last couple of days searching the web and can’t find a simple example to try. Most relate to linking to excel or performing various tasks that I do not require. I’m using ADO recordsets. Any leads to a website for some code would be great.

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Hi Duncan,

    Here's a VBA function that I created to use as an example of looping through rows of data in a Table and then doing something with particular values from each row.
    Let me know if you have any questions.

    Code:
    'Get values from a table using a query in VBA.
    Function Get_DB_Values()
    'Assumes that you have a Table1 and that Field1 is Text and Field2 is a Number.
    ' . . . just for testing purposes.
    ' . . . You should use your actual Table & field names & set your variables to match the data types of your table fields.
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strField1 As String
    Dim intField2 as Integer
    On Error GoTo Error_Handle
       
    Set db = CurrentDb
    strSQL = "Select * From [Table1]"
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
    With rs
    'This Do While loop goes through all the records in strSQL.
    Do While Not rs.EOF
    strField1 = rs![Field1]
    intField2 = rs![Field2]
    'I put this little If Then here with a MsgBox . . . to look at the values 
    'In your case - you will need to put in logic here to update the current record.
    If intField2 = 5 Then
        'MsgBox strField1 & ", " & intField2
    End If
    .MoveNext                               'Move to next record in recordset.
    Loop                                    'Back to 'Do While' to check if we are at the end of the file.
            
    Exit_Get_DB_Values:
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Set db = Nothing
        Exit Function
        
    Error_Handle:
        Resume Exit_Get_DB_Values
    End With
    End Function
    Hope this helps!
    All the best.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Given what you're doing, the most efficient method would be an update query:

    CurrentDb.Execute "UPDATE tblClientAssessmentCDInfection SET DC = True WHERE MRN = " & Me.MRN, dbFailOnError

    which assumes the MRN field is numeric. If you needed a loop, you'd want to loop just on the desired records by opening the recordset on an SQL statement that restricted the records returned to those desired. Tweaking Robeen's code:


    strSQL = "Select * From [Table1] WHERE MRN = " & Me.MRN

    You don't want to loop a table of a million records to update 5.


    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Duncan is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Oct 2010
    Posts
    71
    Thank you for your response. The update query works just fine in updating multiple records. Before I mark this thread as solved is there any advantage in looping though the record sets using the VBA code opposed to the simple update query.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No the query is most efficient, as I said. I'd only use the loop if a query couldn't do the job.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 0
    Last Post: 07-26-2011, 02:03 PM
  2. Msgbox And Search Code Not Working Properly
    By vampyr07au in forum Forms
    Replies: 1
    Last Post: 05-02-2011, 05:16 PM
  3. RunTime Error 3075, code for search button
    By jacie in forum Programming
    Replies: 1
    Last Post: 04-15-2011, 05:23 AM
  4. !!!!Urgent!!!! Search code doesn't work!
    By Laetilae in forum Programming
    Replies: 4
    Last Post: 12-13-2010, 10:34 PM
  5. Replies: 4
    Last Post: 01-11-2010, 11:41 PM

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