dbTalk Databases Forums  

Combo box on an instance of a form

comp.databases.ms-access comp.databases.ms-access


Discuss Combo box on an instance of a form in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mat
 
Posts: n/a

Default Combo box on an instance of a form - 12-12-2009 , 07:05 AM






When I open the form normally, ie: From the database window, the combo
box works correctly. The query behind the combo box references a field
on the form and shows the data. For example:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=[forms]![frmPeople]!
[Nationality]))
ORDER BY tblStates.State_name;

But when I open an instance of the form the combo box can't find the
parameter:
[forms]![frmPeople]![Nationality] and it displays an input box.

I suspect this is because of the nature of the instance of the form.
Is there another way to write the query so that it works for when I
create an instance of the form?

Reply With Quote
  #2  
Old   
Roger
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-12-2009 , 07:11 AM






On Dec 12, 6:05*am, Mat <matthew.... (AT) optusnet (DOT) com.au> wrote:
Quote:
When I open the form normally, ie: From the database window, the combo
box works correctly. The query behind the combo box references a field
on the form and shows the data. For example:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=[forms]![frmPeople]!
[Nationality]))
ORDER BY tblStates.State_name;

But when I open an instance of the form the combo box can't find the
parameter:
[forms]![frmPeople]![Nationality] and it displays an input box.

I suspect this is because of the nature of the instance of the form.
Is there another way to write the query so that it works for when I
create an instance of the form?
in the form's current event, use vba, to create the sql string,
allowing to refer to the 'nationality' field
and assign it to the combox box rowSource

strsql = "select ...."
comboBox.rowsource = strsql

Reply With Quote
  #3  
Old   
Mat
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-12-2009 , 02:49 PM



Yeah I was thinking that code was a solution. I am still hoping there
is a way to write a query though?

Reply With Quote
  #4  
Old   
hbinc
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-12-2009 , 03:58 PM



On Dec 12, 9:49*pm, Mat <matthew.... (AT) optusnet (DOT) com.au> wrote:
Quote:
Yeah I was thinking that code was a solution. I am still hoping there
is a way to write a query though?
Hi Mat,

I do not understand the problem because I do not know what "the
instance of the form" is.
I searched in the Help and I searched in my dictionary, but no clue
how I can relate this to my knowledge of Access on this moment. Can
you explain this for me?

HBinc.

Reply With Quote
  #5  
Old   
Mat
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-12-2009 , 04:27 PM



Sure!

'put this in a module
public collForms as new Collection

'put this code in a button on a form
dim frm as Form
set frm = new Form1 'form1 is the name of a form you have in your
database.
frm.visible = true
collforms.add frm, cstr(frm.hwnd)
set frm = nothing

The above will create multiple copies (instances) of form1.

Reply With Quote
  #6  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-13-2009 , 07:21 AM



Mat <matthew.kay (AT) optusnet (DOT) com.au> wrote in news:0e5e0be0-8fea-4d02-
bbf6-9a5cc08c8523 (AT) x5g2000prf (D...oglegroups.com:

Quote:
Yeah I was thinking that code was a solution. I am still hoping there
is a way to write a query though?

You are creating the form instance via code.
Code is the only solution to make reference to that form
Any attempt to reference the textbox will fail because the query has no
knowledge of the code that created the instance of the form.

Imagine you have 3 open instances of the form, and try to requery one
of those instances. How would the query know which instance to use as
the filter?

--
Bob Quintal

PA is y I've altered my email address.

Reply With Quote
  #7  
Old   
hbinc
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-13-2009 , 10:56 AM



On Dec 12, 11:27*pm, Mat <matthew.... (AT) optusnet (DOT) com.au> wrote:
Quote:
Sure!

'put this in a module
public collForms as new Collection

'put this code in a button on a form
dim frm as Form
set frm = new Form1 'form1 is the name of a form you have in your
database.
frm.visible = true
collforms.add frm, cstr(frm.hwnd)
set frm = nothing

The above will create multiple copies (instances) of form1.
Hi Mat,

I understand. Until now I have never used this kind of things, so I
would like to learn from you for what kind of applications you can use
this feature.

Back to your problem, in your example you created a new form as object
variable frm.
So, in my opinion, you can refer to this variable in your query:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=frm![Nationality]))
ORDER BY tblStates.State_name;

I am not sure, because I never used it, but give it a try.

HBInc.

Reply With Quote
  #8  
Old   
Mat
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-15-2009 , 05:49 AM



I wrote this function, it needs error checking code of course:

Public Function GetControlsValue(strCtl As String) As Variant
Dim frm As Form
GetControlsValue = -1
Set frm = Application.Screen.ActiveForm
GetControlsValue = frm.Controls(strCtl)
End Function

Now I can re-write my queries:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=cstr(GetControlsValu e
("FieldName"))))
ORDER BY tblStates.State_name;

Reply With Quote
  #9  
Old   
hbinc
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-15-2009 , 06:31 AM



On Dec 15, 12:49*pm, Mat <matthew.... (AT) optusnet (DOT) com.au> wrote:
Quote:
I wrote this function, it needs error checking code of course:

Public Function GetControlsValue(strCtl As String) As Variant
Dim frm As Form
GetControlsValue = -1
Set frm = Application.Screen.ActiveForm
* * * * GetControlsValue = frm.Controls(strCtl)
End Function

Now I can re-write my queries:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=cstr(GetControlsValu e
("FieldName"))))
ORDER BY tblStates.State_name;
Hi Mat,

And what if you use instead of

cstr(GetControlsValue("FieldName")

just

Screen.ActiveForm("FieldName")

In that case GetControlsValue is not necessary, and also not an
instance of the form.


HBInc.

Reply With Quote
  #10  
Old   
hbinc
 
Posts: n/a

Default Re: Combo box on an instance of a form - 12-15-2009 , 07:52 AM



On Dec 15, 12:49*pm, Mat <matthew.... (AT) optusnet (DOT) com.au> wrote:
Quote:
I wrote this function, it needs error checking code of course:

Public Function GetControlsValue(strCtl As String) As Variant
Dim frm As Form
GetControlsValue = -1
Set frm = Application.Screen.ActiveForm
* * * * GetControlsValue = frm.Controls(strCtl)
End Function

Now I can re-write my queries:

SELECT tblStates.State_name, tblCountries.Country_name
FROM tblCountries INNER JOIN tblStates ON tblCountries.ID =
tblStates.tblCountriesID
WHERE (((tblCountries.Country_name)=cstr(GetControlsValu e
("FieldName"))))
ORDER BY tblStates.State_name;
Hi Mat,

In your example you used Screen.ActiveForm.
Personally I do not like "Screen". It is the_active_screen, whatever
it is. In almost all cases you can use the variable of current form:
Me.

Sometimes you need to refer to the previous from.
I do that by using the OpenArgs argument in the Open statement of the
new form. The OpenArgs argument has the name of the from_where_form,
i.e. Me.Name.
In the new opened form I then can define

Dim prev_form as Form
Set prev_form = Forms(Me.OpenArgs).

Now I have access to all Controls or Properties of the prev_form.
You can even do this many levels deep.

HBInc.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.