Discussion Locked

VB and Access 2000 Forms

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

VB and Access 2000 Forms
I have a text box in one form in a Access database and I am wanting to share values. I am wanting the value from:

forms![Form1]![text1]

to be able to share this value with a form in the same database such as:

forms![Form2]![text1]

I have accomplished this as a private statement within sharing the value in a ElseIf statement on the same form. But I am not doing something write in my coding on the public statement to share the value within the same .mdb file. Could someone please help me with an idea or an example of some code that I may be able to look at and interpret for my use of my application I am building. Thanks for any help available.

10 replies

by donq

You have two options that immediately come to mind:

One: Build a multi-table query that produces the complete recordset you are after and use the query name as the Forms record source property. If you're entering data in the form make sure eachfield that can be edited comes from the querie's primary table, and referenced fields don't take focus but rather just display referenced data.

Two: Within the "target" Form enter a default value as "Forms![DataSourceFormName].[FieldToDispayName]" in the field you want the shared value to show up in.

Good Luck and ALWAYS place your code within the Target Form so it is portable.

by glen_mcleod

An alternative to directly referencing controls on different forms is to use property procedures. If you want to make a certain text box value available, you just code the property, example:

Public Property Get ClientName() as String
ClientName = Me!txtClient.Text
End Property

This encapsulates the textbox access within the form, so other forms (& classes, modules) don't have to know what the textbox is called, they just access the property as in:

Me!txtClient.Text = frmClient.ClientName

Judging by what's happening to the next version of VB (and Office just may follow), controls are no longer going to be public parts of forms anymore and you'll have to go this route anyway.

Glen

by bob sellman

If you want to carry the value throughout your application, then try the following:

First, set up a module which has the following statements:

Public strText1 as String
Public strText2 as String

Next, in each form, put appropriate VBA code behind each of the controls (such as the AfterUpdate property). If you are displaying records, you might also put the same code for the OnCurrent property of each form.

The code would set the value of the appropriate variable to the value of the control on the form, such as:
strText1 = me.Text1 (on Form1)
and
strText2 = me.Text2 (on Form2)

You can these retrieve these values any place in your application by using the values in these variables. The only caveat is that there will be no values in each variable until its associated form has been opened.

by bob sellman

Here's an additional point I'd like to mention as a reminder, in case you are forgetting about it:

If you are constructing SQL statements or filter/where clauses with either the public variables I suggested using or with the direct reference, youmust surround the value with single quotes if it is a text value (or with # sign if a date value). Example:

Dim strFilter as String

strFilter = "[Column1] = '" & strText2 & "'"

If the value of strText2 is MyText, then the resulting statement would read:
[Column1] = 'MyText'

by whyong

if you want to get a value from another form, just can just put "=[Forms]![form1].[text1]" in your Form2.Text1.ControlSource, form2.text1 will get the value from form1.text1, provided you are opened form1 as well. hope that this is what you want, happy coding.

This discussion is closed to new replies.