Solved

MS Access - Docmd.Openform pass a variable

Posted on 2013-12-11
8
2,779 Views
Last Modified: 2013-12-11
Hello all!
Using MS Access 07

I have a Form where if the user clicks a control button I have it Open a 2nd form.  

On that 2nd Form there are two Unbound combo boxes that the user selects a State or County to filter  info on that 2nd form.  

That same State and County had already selected on the 1st Form (also in unbound combo boxes) before they clicked on the control button.

The 2nd Form is Not a subform of the 1st.  It is an independent form.

On both Forms the Unbound State combo box is named "cboSelectState" and the Unbound County combo box is named "cboSelectCounty"

My OnClick event on the 1st Form is

Private Sub cmdAddStateCountyBkType_Click()
Dim lngState As Long, lngCountyCode As Long
lngState = Me.cboSelectState.Column(0)
lngCountyCode = Me.cboSelectCounty.Column(0)

    DoCmd.OpenForm "frmSys_ValidCountyCodeBookType", , "[cboSelectState]=" & lngState & "," & "[cboSelectCounty]=" & lngCountyCode    

End Sub

Open in new window


It opens the Form correctly but it doesn't update those two combo boxes on the 2nd from with the info I'm trying to pass to it.
0
Comment
Question by:wlwebb
  • 5
  • 2
8 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 39710931
The OpenForm method takes several arguments. You're using the WHERE argument in your example, which will not update or modify your combos.

To do that, you can use the OpenArgs argument, or just refer to the other form directly if you always want the combos on  frmSys_ValidCountyCodeBookType to always show the values from the first form.

To use OpenArgs, you'd change the OpenForm to this:

DoCmd.OpenForm "frmSys_ValidCountyCodeBookType", , , , , , lngState & ";" & lngCountryCode

Open in new window

This would pass in two values, separated by a semicolon.

Now in the Open or Load event of frmSys_ValidCountyCodeBookType:

If Nz(Me.OpenArgs, "") <> "" Then
  If Instr(0, Me.OpenArgs, ";") > 0 Then
    Dim arr() As String
    arr() = Split(Me.OpenArgs, ";")

    Me.cboSelectState= arr(0)
    Me.cboCountryCode= arr(1)
  End If
End If

Open in new window

Assuming the values exist in those combos, this should set them to the values you pass in.
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 39710932
The WHERE condition is for filtering data in the form's recordsource, not for setting control values, and does not affect unbound controls.  Try this instead:
Private Sub cmdAddStateCountyBkType_Click()
Dim lngState As Long, lngCountyCode As Long
lngState = Me.cboSelectState.Column(0)
lngCountyCode = Me.cboSelectCounty.Column(0)

    DoCmd.OpenForm "frmSys_ValidCountyCodeBookType"
       Forms!frmsys![cboSelectState]=  lngState 
       Forms!frmSys![cboSelectCounty]= ngCountyCode    

End Sub

Open in new window

0
 

Author Comment

by:wlwebb
ID: 39710943
LSM...
I get a Run-Time Error "5"
Invalid procedure call or argument.
it's stopping on
  If InStr(0, Me.OpenArgs, ";") > 0 Then

when I hover over the "OpenArgs" in the first line I do see the correct cbo codes from the 1st form...... ????
0
 
LVL 84
ID: 39710949
Sorry, my .NET is showing :)

Change that line to this:

If InStr(1, Me.OpenArgs, ";") > 0 Then
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:wlwebb
ID: 39710952
Mbiz....
That works for passing the State.....

However, on the County it isn't working.  That's because my combo box rowsource for the County is updated from the AfterUpdate event of the cboSelectState combo box.....

Since the Rowsource isn't within the Rowsource of that combobox but is in the VB code can I get there?
0
 

Author Comment

by:wlwebb
ID: 39710955
LSM....
Well that works for the State, but like my response to Mbiz just above, my Rowsource code for the SelectCounty combobox is in VB code of the AfterUpdate event of the SelectState......  so the passing of that argument doesn't see any rowsource....

Is there a way around?
0
 

Author Comment

by:wlwebb
ID: 39710960
NEVER MIND..........

Got it....  I just copy and inserted the "SELECT" code for the RowSource from the CboSelectState just before the "me.cboselectCounty = arr(1)" coding
0
 

Author Closing Comment

by:wlwebb
ID: 39710963
Thanks LSM and MBiz.......

I tried both methods and they both worked!!!!!!!  I gave you each equal amounts of points because both worked and both were posted extremely quickly after I posted the question

THANK YOU
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now