简体   繁体   中英

Button searches subform, then finds record in main form (Access VBA)

I have this ACCESS form that contains a subform: Form & Subform I'm trying to create a search button (using VBA) to find a student by name (students' names are not in the table behind the main form).

I've done the first step, which is to search the subform for a student's name, but I'm having trouble with my desired second step. I'd like the code to then take the CWID number of the student and find the matching record in the main form. How would I do this? (My current code is below)

I've tried DoCmd FindRecord and GoToRecord, but it is completely stumping me. I'm Google-learning how to do this and think I'm fundamentally misunderstanding something and am thus unable to search for or understand the answer. Any help would be greatly appreciated.

Private Sub btnSearch_Click()
Dim SQL As String
SQL = "SELECT [AWN Banner].CWID, [AWN Banner].FirstName, [AWN Banner].LastName, [AWN Banner].Freshman, [AWN Banner].Instructor, [AWN Banner].Course " _
    & "FROM [AWN Banner] " _
    & "RIGHT JOIN [AWNEntry] ON [AWN Banner].CWID = [AWNEntry].CWID " _
    & "WHERE [LastName] LIKE '" & Me.txtKeywords & "*' " _
    & "ORDER BY [AWN Banner].LastName "

    Me.subAwnObj.Form.RecordSource = SQL
    Me.subAwnObj.Form.Requery
    End Sub

When I create my search boxes I put in the underlying form source a WHERE clause WHERE [field] LIKE "*" & [MySearchBox] & "*" I put such a clause in the subform and the main form I create a JOIN to the subform record with the same WHERE clause defined. Then on click I request all recordsources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM