borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (OPENNING FILES)Next Topic (DataGrid Error) New Topic New Poll Post Reply
AndreaVB Forum : Database : How do I Pass Data to another Form? Solved Topic
Poster Message
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39

icon How do I Pass Data to another Form?

I am working on a project using VB in MS Access 2003.
I have 2 forms:
SupportersForm & AddDonationForm
SupportersForm has a button which is used to launch AddDonationForm.

I need to pass the Supporter ID from SupportersForm to AddDonationForm so that AddDonationForm can write the Donation data to the db.

The examples that I have found on MSDN show how to do this if you instantiate a new form (like Form2) in the first form's button_click method.  This is confusing to me since I want to launch an existing form (AddDonationForm).

I also tried to see if I could reference another form's control (txtSupporterID = SupportersForm.Supporters.ItemData(0)) but the second form does NOT recognize the first form and I get an Object Required error at run-time.

Is there a way to add a parameter to the OpenForm() or an equivalent Form load method?

or

... I guess I could usde a global, but that seems inefficient

or

What is the best way to do this?

I appreciate any help that can be provided.

Thanks,
Bill

19-05-2006 at 04:29 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I Pass Data to another Form?

Hello.

quote:
ba1959nh wrote:
I need to pass the Supporter ID from SupportersForm to AddDonationForm
[...CUT...]
What is the best way to do this?
Create a public variable in AddDonationForm, and set it from SupportersForm when the ID is set.
Proceed like following:
=== into AddDonationForm code ===
Option Explicit

Public SuppID as Long

Private Sub Form_Load()

    ' [... other code...]

    txtSupporterID.Text = SuppID
End Sub

=== into SupportersForm code ===
Private Sub btnOpenDonationForm_Click()

    ' [... other code...]

    AddDonationForm.SuppID = Me.txtCurrentID.Text
    AddDonationForm.Show
End Sub
Then you can workaround the variable value into the AddDonationForm form.

Hope it helps


____________________________
Real Programmer can count up to 1024 on his fingers
19-05-2006 at 06:37 PM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: How do I Pass Data to another Form?

Hi yronium......

I am getting an Object Required error ar runtime when AddDonationForm.Show is called.

Any ideas?

Thanks,
Bill

19-05-2006 at 08:24 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I Pass Data to another Form?

Hello

quote:
...SupportersForm has a button which is used to launch AddDonationForm...
what code are you using to launch the second form? The idea is to set the variable right before opening the second form.
I post the code I use in a project of mine.
' open the Destination window
Private Sub btnDestination_Click()
    Dim frm As frmDestination
    Set frm = frmDestination
    frm.ClientCode = Me.CurrentID
    frm.Top = Me.Top + 100 ' + Me.Height - frm.Height
    frm.Left = Me.Left + Me.Width + 100 - frm.Width
    frm.Hide
    frm.Show
    Set frm = Nothing
End Sub
CurrentID is a module level variable of current form, ClientCode is a public variable of frmDestination. Of course it works fine.
I prepare a small sample project in which you can write a text in form1 and pass it to form2 or form3. Please find it attached and enjoy.

____________________________
Real Programmer can count up to 1024 on his fingers

____________________________
Attached:
PassingValues.zip 3 KB (Downloads: 3)
20-05-2006 at 11:08 AM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: How do I Pass Data to another Form?

OK.
Here is where I am confused.....
It looks like you are declaring and instantiating Form2 on-the-fly in your Form1 code.

My 2 forms were created using the Access 2003 Form Designer.......so.....the 2nd form (AddDonationForm) has already been declared (I believe)....which seems to make it NOT visible in the 1st Form's (SupportersForm) VB code via the VB 6.3 IDE.  

For example, when I add the following code in SupportersForm buttonClick event,

    Dim frm As AddDonationForm
    Set frm = AddDonationForm

I get the following error:
Compile Error:
User-defined type not defined



It appears that both forms do not have the scope to see eachother.....if that makes sense.

Here is the button click code originally created by the Build Event Wizard:

Private Sub btnLaunchAddDonationForm_Click()
On Error GoTo Err_btnLaunchAddDonationForm_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "AddDonationForm"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_btnLaunchAddDonationForm_Click:
    Exit Sub

Err_btnLaunchAddDonationForm_Click:
    MsgBox Err.Description
    Resume Exit_btnLaunchAddDonationForm_Click
    
End Sub


Am I missing something here?

I appreciate your patience and assistance.  

Please Advise,
Bill

20-05-2006 at 11:57 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I Pass Data to another Form?

quote:
Am I missing something here?
You have missed just to specify that you're not working with VB but with VBA, and your IDE is not VB IDE but MSAccess IDE.
Anyway, nevermind the VBA language I only want you to understand the principle. My procedure to open a form by declaring it as an instance of an existing Form object rather than directly open it, does not matter that much.
quote:
Private Sub btnLaunchAddDonationForm_Click()
On Error GoTo Err_btnLaunchAddDonationForm_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "AddDonationForm"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_btnLaunchAddDonationForm_Click:
    Exit Sub

Err_btnLaunchAddDonationForm_Click:
    MsgBox Err.Description
    Resume Exit_btnLaunchAddDonationForm_Click
    
End Sub
So you open another existing form. The goal is to set a variable of it during opening.
In VB you have to set it before opening it, but I'm not sure you can still do it before in VBA. However, try modifying you code like following
Private Sub btnLaunchAddDonationForm_Click()
On Error GoTo Err_btnLaunchAddDonationForm_Click
    AddDonationForm.SuppID = Me.txtCurrentID.Text
    DoCmd.OpenForm AddDonationForm
Exit_btnLaunchAddDonationForm_Click:
    Exit Sub
Err_btnLaunchAddDonationForm_Click:
    MsgBox Err.Description
    Resume Exit_btnLaunchAddDonationForm_Click
End Sub
It should work. If it doesn't, try switching the variable assigning instruction and the DoCmd.OpenForm line. But in this case you first open the form and then set its public variable. It means that, if you need that value to filter records, you got to requery the form after have passed the value.

I have no time now to try it in Access, as I got to go out, but you can do some tryouts by yourself. Just remember that VB is not the same of VBA, and Access forms are not the same of VB forms.

Let me know.

____________________________
Real Programmer can count up to 1024 on his fingers
20-05-2006 at 02:03 PM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: How do I Pass Data to another Form?


I tried your code.

I have
Public SuppID As Long
at the top of the AddDonationForm code
(outside of any Sub or Function)

I pasted your code in my SupporterForm

With the calls you mentioned ordered this way...

  AddDonationForm.SuppID = Me.txtCurrentID.Text
  DoCmd.OpenForm AddDonationForm


I get the following error at runtime when I click the button:
Object Required
NOTE: the error dialog frame title is:
Microsoft Office Access

If I change the order of those 2 lines:

  DoCmd.OpenForm AddDonationForm
  AddDonationForm.SuppID = Me.txtCurrentID.Text


I get the following error:
Microsoft Office Access
The action or method requires a Form Name argument


I apologize about the VB versus VBA......
When I select View - Code from Access 2003
the pgm launched says Microsoft Visual Basic
(in the title bar) and when I click Help - About, it says:
Microsoft Visual Basic 6.3.

Could Access be launching the wrong VB application?

H E L P



Thanks again for your patience.....

Bill


20-05-2006 at 06:56 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I Pass Data to another Form?

OK, I'm back (... I hate the tricky kranky Access' VBA!...)
I prepared a sample .mdb, please find it attached here.
It doesn't use a form-level public variable, but an external module variable. There surely is a way to use a form variable too, but I don't want to spend more time trying to find it. The external module works.
The .mdb version is Access2k, so you got to convert it.
Hope it helps.

____________________________
Real Programmer can count up to 1024 on his fingers

____________________________
Attached:
db1.zip 22 KB (Downloads: 4)

20-05-2006 at 10:03 PM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: How do I Pass Data to another Form?

yronium,


That worked great !!!
Thanks for the nice example.

- Bill




[Edited by ba1959nh on 21-05-2006 at 03:25 PM GMT]

21-05-2006 at 03:24 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: How do I Pass Data to another Form?

You're welcome


____________________________
Real Programmer can count up to 1024 on his fingers

21-05-2006 at 06:38 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : How do I Pass Data to another Form? Solved Topic
Previous Topic (OPENNING FILES)Next Topic (DataGrid Error) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder