473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Opening form with Value in Combo Box from another form

I have a form where the user selects an item from a list box, and then
works on that item. The user chooses an AD, then opens a form to
assign departments to the ad. The top of the Depts form has a combo
box, to select an ad from the drop down list.

I would like the Depts form to open with the Ad selected on the Main
form displaying in the combo box, AND any information already added
presented to the user. (I am thinking this latter part would be a
requery of the combo box or something).

My problem is that I can't get the frmDeptsForAd to open with a value
in the combo box.

Perhaps my form design/approach is wrong. I have a combo box at the
top so the user can update any ad without going back to the main form.

Thanks!
sara

Code:
Private Sub cmdDeptsForAd_Click()
On Error GoTo Err_cmdDeptsForAd_Click

Dim strDocName As String
Dim strLinkCriteria As String
Dim lngAdKey As Long

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName =
"frmSelDeptsForAd","Forms!frmSelDeptsForAd!cboAdKe y.Value =" & lngAdKey
'Have tried many forms of this line of code - nothing worked so far

DoCmd.OpenForm strDocName

Exit_cmdDeptsForAd_Click:
Exit Sub

Err_cmdDeptsForAd_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdDeptsForAd_Click

End Sub
"Forms!frmSelStoresForAd!cboAdKey.Value =" & lngAdKey

May 5 '06 #1
7 12203
sara wrote:
I have a form where the user selects an item from a list box, and then
works on that item. The user chooses an AD, then opens a form to
assign departments to the ad. The top of the Depts form has a combo
box, to select an ad from the drop down list.

I would like the Depts form to open with the Ad selected on the Main
form displaying in the combo box, AND any information already added
presented to the user. (I am thinking this latter part would be a
requery of the combo box or something).

My problem is that I can't get the frmDeptsForAd to open with a value
in the combo box.

Perhaps my form design/approach is wrong. I have a combo box at the
top so the user can update any ad without going back to the main form.

Thanks!
sara

Code:
Private Sub cmdDeptsForAd_Click()
On Error GoTo Err_cmdDeptsForAd_Click

Dim strDocName As String
Dim strLinkCriteria As String
Dim lngAdKey As Long

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName =
"frmSelDeptsForAd","Forms!frmSelDeptsForAd!cboAdKe y.Value =" & lngAdKey
'Have tried many forms of this line of code - nothing worked so far

DoCmd.OpenForm strDocName

Exit_cmdDeptsForAd_Click:
Exit Sub

Err_cmdDeptsForAd_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdDeptsForAd_Click

End Sub
"Forms!frmSelStoresForAd!cboAdKey.Value =" & lngAdKey

The format is
Docmd.OpenForm "FormName",,,"Dept = " & Me.Combo

Here's another method
Docmd.OpenForm "FormName",,,,,,Me.Combo

What's the difference? The first opens the form and filters to what you
have passed.

The second opens the form with an argument. In your events you can then
use the value by looking at what is contained in OpenArgs in the second
form. Ex:
If not isnull(Me.OpenArgs) then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.Combo = Me.OpenArgs
Endif

May 5 '06 #2
Thanks, Salad - but I don't quite have it yet. I'm still quite new at
all this coding (Sorry - didn't mention this at first).

Here's the code I tried:
From the Main form:

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName = "frmSelDeptsForAd"
DoCmd.OpenForm strDocName, , , , , ,
"Forms!frmSelDeptsForAd!cboAdKey"

On the SelectDepts form On Open event:
Private Sub Form_Open(Cancel As Integer)

' If the user came from the main form and selected an ad, put that ad
in the combo box

If Not IsNull(Me.OpenArgs) Then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.cboAdKey = Me.OpenArgs
End If
End Sub
And the first way (I commented out the On Open event code for this
try):
DoCmd.OpenForm strDocName, , , "AdKey = " &
"Forms!frmSelDeptsForAd!cboAdKey"

I managed to get the quotes, and format the parameter properly, at
least I got it to compile. But when the SelDepts form opens, the combo
box is still blank. Can you see what I'm missing?

thanks - I relaxed quite a bit when I saw your response - Help is here!
sara

May 5 '06 #3
This is pretty simple. Use the OpenArgs Property of the form. In the
main form add something like this to a command button:

Private Sub Command4_Click()
DoCmd.Openform "Form4", acNormal, , , , , comboAD
End Sub

Then in the Dept form - in the Load or Open event you can do this:

Private Sub Form_Load()
ComboDept = Me.OpenArgs
End Sub

You can spruce this up a bit by checking for null values in openargs
first:

If Not IsNull(Me.OpenArgs) Then
ComboDept = Me.OpenArgs
End IF

Rich

*** Sent via Developersdex http://www.developersdex.com ***
May 5 '06 #4
sara wrote:
Thanks, Salad - but I don't quite have it yet. I'm still quite new at
all this coding (Sorry - didn't mention this at first).

Here's the code I tried:
From the Main form:

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName = "frmSelDeptsForAd"
DoCmd.OpenForm strDocName, , , , , ,
"Forms!frmSelDeptsForAd!cboAdKey"

On the SelectDepts form On Open event:
Private Sub Form_Open(Cancel As Integer)

' If the user came from the main form and selected an ad, put that ad
in the combo box

If Not IsNull(Me.OpenArgs) Then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.cboAdKey = Me.OpenArgs
End If
End Sub
And the first way (I commented out the On Open event code for this
try):
DoCmd.OpenForm strDocName, , , "AdKey = " &
"Forms!frmSelDeptsForAd!cboAdKey"

I managed to get the quotes, and format the parameter properly, at
least I got it to compile. But when the SelDepts form opens, the combo
box is still blank. Can you see what I'm missing?

thanks - I relaxed quite a bit when I saw your response - Help is here!
sara

In your second forms OnOpen event...to see what you passed...you could
enter a line like
msgbox "Openargs = " & Me.OpenArgs
and it will display what you sent to it...or nothing but a blank msgbox
if null.

I expect it would show "Forms!frmSelDeptsForAd!cboAdKey". That's not
what you want.

Your code to open the form should be something like
DoCmd.OpenForm strDocName, , , , , ,Me.cboAdKey

This passes the value, not the string "Forms!frmSelDeptsForAd!cboAdKey".

The following.
DoCmd.OpenForm strDocName, , , "AdKey = " & _
"Forms!frmSelDeptsForAd!cboAdKey"
Is also incorrect, unless you have an adkey value equal to the string
"Forms!frmSelDeptsForAd!cboAdKey".

Here is a hint. Strings are surrounded in ". Date in #. Number in
nothing. Ex:
str = "Smith"
dat = #1/1/2006#
num = 2
"LastName = """ & strt & """
or
"Lastname = '" & str & "'"
"StartDate = #" & dat & "#"
"Qty = " & num

Basically, the "where" clause you pass in the Docmd.Openform is like a
SQL whereclause without the word "Where".

Hope this gets you a bit further.


May 6 '06 #5
Salad - (and Rich P, too!)
Sorry for the delay - I was out for a week.

I have (again) tried to make this work and am still not there. I think
one problem may be about OpenArgs. Do I declare that somehow? I
checked other posts and it appears Args was declared as a string, but
then how does Me.OpenArgs work? Me.OpenArgs is always null for me - no
matter which code I have tried.

Summary of what I've learned through reading (I have finally gotten my
Access Help fixed): - Maybe I have misunderstood something here?
OpenArgs is only available with DoCmd.OpenForm, which I'm using.
OpenArgs is the last parameter of the OpenForm method (which explains
the ",,,," in your code for me.
You use either OpenArgs or Where condition (as you showed above)

So, I am baffled as to why I am not seeing the OpenArgs in my msgbox
(and why the ad it not showing up in the drop down box)
Code for calling form:
AdKey = Me.txtAdKey

strDocName = "frmSelDeptsForAd"

strLinkCriteria = "[AdKey] = " & AdKey

DoCmd.OpenForm strDocName, , , , , , AdKey
Code for Second form: (frmSelDeptsForAd)
Private Sub Form_Load()
On Error GoTo Err_Form_Load

Set mdb = CurrentDb
Set mqdf = New DAO.QueryDef

Me.cboAdKey = Me.OpenArgs
MsgBox "OpenArgs = " & Me.OpenArgs

Thanks for bearing with me on this one. Your comments make it look so
doable, but I'm just not getting it!
sara

May 16 '06 #6
Hi sara,

You might try something real basic/simple.

In the mainform under some button, just open a form

DoCmd.OpenForm "Form2", acNormal, , , , , "testing"

Note: I am using the constant acNormal. You might try that also.

Then in the form you open - in the Form_Load event

MsgBox Me.OpenArgs

I just did this without any issues. I got the message box with
"testing" as text on the open event of Form2.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
May 18 '06 #7
Thanks. I got it! I don't know why I couldn't do it before, but I am
now all set. Couldn't have done it without your (and Salad's) generous
help.

Moving on to my next issue.

Again, thank you!
Sara

May 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard | last post by:
Hi, I have a form based on a table. When I filter the form I want to run a report based on the same table with the same filter as the form. No problem until I want to filter a combo box where...
1
by: Caesar Augustus | last post by:
Greetings all! Before I ask a question I will try to provide a little background. I am currently working on an app(PD) that is essentially a search engine to find specific records. Once the...
1
by: Todd | last post by:
My form contains a combo box and the "not in list" event is triggerred when the user enters a value that is not among the values listed in the combo box. I want the user to be able to add a new...
17
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main form has code in the AfterUpdate event which adds a...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
3
by: dufnobles via AccessMonster.com | last post by:
I have a form that displays a dropdown box. The options in the drop down vary according to a value queried from a database boolean (Yes/No) field. I would like to do the following: If the...
1
by: omar.norton | last post by:
I am trying to create a from with a series of combo boxes that each query a different field (called Specific01, Specific02 etc., except the first field which is called Condition). Each combo box...
1
by: abhishekbrave | last post by:
The code below is opening a calendar on mouse over in the same window. I need the calendar to be opened in new window. Have to fulfill this requirement urgentely so posting the whole code here. I...
11
by: casan.mike | last post by:
Hi all, I have a form called "customer_frm" with many records in it. I would like to have a form that has a combobox listing all the customers, and a button that open the "customers_frm" to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.