Link to home
Start Free TrialLog in
Avatar of kes3
kes3

asked on

VBA script to dynamically change the SQL "Record Source" query on a form using radio buttons

At the moment I have 3 forms all with the same graphical presentation.
They query the same data set.
The only difference between the forms is the "Record Source" SQL statement used.

I'd like to consolidate these forms into one form with three radio buttons allowing selection of the "Record Source" SQL statement with the appropriate data refresh that is necessary afterwards.

Does anyone have the VBA for this.

Regards

Kes3

ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kes3
kes3

ASKER


Thanks Pete

I inserted an "option group" with three radio buttons. Then, again in design view I selected the outer perimeter of this "option group", right clicked and chose properties. This shows the "option group" has    Name = Frame43.
I then clicked the "Event Tab" and Selected the three dots ... to the right of the "After Update" box and inserted your code amended as you suggest:

Private Sub Frame43_AfterUpdate()

Dim rs1, rs2, rs3
rs1 = "SELECT Issues.*, Contacts.[E-Mail Address] AS [assigned to e-mail], Contacts_1.[E-Mail Address] AS [opened by e-mail] FROM Contacts AS Contacts_1 INNER JOIN (Contacts INNER JOIN Issues ON Contacts.ID = Issues.[Assigned To]) ON Contacts_1.ID = Issues.[Opened By];"
rs2 = "OPEN ISSUES"
rs3 = "SELECT Issues.*, Contacts.[E-Mail Address] AS [assigned to e-mail], Contacts_1.[E-Mail Address] AS [opened by e-mail] FROM Contacts AS Contacts_1 INNER JOIN (Contacts INNER JOIN Issues ON Contacts.ID = Issues.[Assigned To]) ON Contacts_1.ID = Issues.[Opened By];"

Me.Dirty = False ' saves any unsaved changes

Select Case Me.Frame43
Case 1
Me.RecordSource = rs1
Case 2
Me.RecordSource = rs1
Case 3
Me.RecordSource = rs1
End Select

End Sub

I then tried it and Access does make calculations when I select any of the radion buttons howeve it produces the same result each time which is not what should happen.

Any ideas ?

Reagrds

Kes3
{points to peter pls.}
you  are using the same recordsets for your recordsource
change rs1 to rs2 for case 2 and rs1 to rs3 for case 3

Select Case Me.Frame43
Case 1
Me.RecordSource = rs1
Case 2
Me.RecordSource = rs2
Case 3
Me.RecordSource = rs3
End Select

Sorry - my mistake -copy and paste strkes again!
You should be using rs1, rs2 and rs3 in the Case statement.
Select Case Me.Frame43
Case 1
Me.RecordSource = rs1
Case 2
Me.RecordSource = rs2
Case 3
Me.RecordSource = rs3
End Select

Pete
Avatar of kes3

ASKER



Many Thanks folks. I was about to paste rs1 ...rs3 necessary changes back here ....

Regards

Kes3