Answered by:
Loop through a subform

Question
-
Hello
I have created a relational database of organisations, offices and employees. One organisation has many offices, and one office has many employees. All good on that score.
I have a main form (frmContacts derived from tblOrganisation) and a sub form (subfrmOffice derived from tblOffice). Don't worry about the employees.
Each office record has an OfficeID (primary key). The tblOrganisation primary key as a foreign key in tblOffice.
I have created a command button (visual basic code) on the MainForm (frmContacts) whereby the user enters the OfficeID as InputData1, and the form navigates to the particular record. The code is:
DLookup("[FKOrgID]", "tblOffice", _
"[OfficeID] = " & InputData1)
The command works perfectly well if one organisation only has one office. However, if an organisation has several offices, the form will only navigate to the organisation and the first office attached to that organisation. The user then has to manually click "Next Office Record " button (in the subfrmOffice) to find the particular office with the RN the user input.
In otherwords, say an organisation has three offices, with OfficeID 1, OfficeID 2 and OfficeID 3. If I want to go to OfficeID 3, I currently click the command button I created on the main form (frmContacts), input 3, and the form displays the organisation and OfficeID 1. I then manually click the subform nextrecord button to get to OfficeID 3.
How can I get the command to go straight to the organisation along with office OfficeID 3?
The following code nearly works, but the loop loops through the main form and not the subform, and of course then does not stop:
Dim InputData1 As Integer 'User inputs OfficeID
Dim InputData2 As IntegerMe!subfrmOffice.Form.txtboxOfficeID.SetFocus
InputData2 = Me!subfrmOffice.Form.txtboxOfficeID.ValueIf InputData1 = InputData2 Then
Exit Sub
Else:
Do While (InputData1 <> InputData2)
Me!subfrmOffice.Form.txtboxOfficeID.SetFocus
Call GoToNextRecord
Me!subfrmOffice.Form.txtboxOfficeID.SetFocus
InputData2 = Me!subfrmOffice.Form.txtboxOfficeID.Value
Loop
End IfPublic Sub GoToNextRecord()
DoCmd.GoToRecord , , acNextAs I said, it nearly works, but the loop is the problem.
- Moved by Dave PatrickMVP Tuesday, March 20, 2018 12:41 PM
Tuesday, March 20, 2018 12:19 PM
Answers
-
I'd ask for help over here.
https://social.msdn.microsoft.com/Forums/Lync/en-US/home?forum=accessdev
https://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Guido Franzke Tuesday, March 20, 2018 12:48 PM
- Marked as answer by Richard MuellerMVP, Banned Tuesday, March 27, 2018 12:32 PM
Tuesday, March 20, 2018 12:43 PM -
I would prefer the forum for Access developers. I don't think it's a problem of VB.
https://social.msdn.microsoft.com/forums/office/en-us/home?forum=accessdev
- Marked as answer by Richard MuellerMVP, Banned Tuesday, March 27, 2018 12:32 PM
Tuesday, March 20, 2018 12:49 PM
All replies
-
I'd ask for help over here.
https://social.msdn.microsoft.com/Forums/Lync/en-US/home?forum=accessdev
https://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Guido Franzke Tuesday, March 20, 2018 12:48 PM
- Marked as answer by Richard MuellerMVP, Banned Tuesday, March 27, 2018 12:32 PM
Tuesday, March 20, 2018 12:43 PM -
I would prefer the forum for Access developers. I don't think it's a problem of VB.
https://social.msdn.microsoft.com/forums/office/en-us/home?forum=accessdev
- Marked as answer by Richard MuellerMVP, Banned Tuesday, March 27, 2018 12:32 PM
Tuesday, March 20, 2018 12:49 PM