?
Solved

Passing values between forms

Posted on 2003-03-17
18
Medium Priority
?
185 Views
Last Modified: 2011-01-20
Hey All,
Ok, here's the way it goes. (VBA btw) I have 4 forms, we'll call em, Form1, Form2, Form3, Form4.  Form1 loads first, then 1 shows 2, 2 shows 3, and 3 shows 4.  So I have a value that i want to pass between these forms and I've tried something like
(form1)
public sub commanbutton1.click
     form2(passvar).show
end sub
(form2)
public sub userform_initialize(passvar as integer)
...
end sub

the thing is that I keep getting an error that says "event procedure declaration does not match description of event having the same name."  is there a way to pass the variables?  I've also got these 4 forms that have been created, and I was reading something that mentioned parent and child forms, but didn't go in depth into what they were.  would this have any applicability in my situation?
0
Comment
Question by:kidgenius2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 3
  • +1
18 Comments
 
LVL 1

Expert Comment

by:JH0401
ID: 8154442
you can create a module with global variables.  these variables may be passed between forms.

Module1

Option Explicit

Global MyVariable1 as String 'or whatever variable type
Global MyVariable2 as String 'or whatever variable type

the above variables will be visible to all forms in project.  
0
 

Author Comment

by:kidgenius2002
ID: 8154463
is there  a way to do it without doing the module though? i have no module level stuff that i'm doing, it's all in the forms, so I was hoping not to have to load a module if i didn't need to.
0
 
LVL 1

Expert Comment

by:JH0401
ID: 8154479
why no module???
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kidgenius2002
ID: 8154502
is there  a way to do it without doing the module though? i have no module level stuff that i'm doing, it's all in the forms, so I was hoping not to have to load a module if i didn't need to.
0
 

Author Comment

by:kidgenius2002
ID: 8154508
all my calculations and such are being done from the forms.  it's just the way it's being done.  if i needed, i guess i could do a few things at the module level, but i don't really need to.  each form has 2 events, the click event and teh initialize event.  and some loops done in each of those.
0
 
LVL 1

Expert Comment

by:JH0401
ID: 8154528
I think it would be easiest just to create a module and declare all the variables that you want to pass from form to form in that module!
0
 

Author Comment

by:kidgenius2002
ID: 8154542
but i don't want to run a module just for the sake of declaring variables, it seems like a waste of memory/time to have to load a seperate module to just load variables.  it's also not very convenient to do it either, in this particular case at least.
0
 

Expert Comment

by:DroopyA
ID: 8154554
It would be much easier to create a modual, but if you really want to avoide it...

1. Form 1 has a public variable.
(Public m_Form1PublicVariable As Integer)

2. In Form2_have the same public variables
(Public m_Form2PublicVariable As Integer)

3. Then in Form2_Activate call GetForm1Info.

4. GetForm1Info would looks something like:

Public Sub GetForm1Info()
     m_Form2PublicVariable = Form1.m_Form1PublicVariable
     Unload Form1
End Sub

Peace
-N473
0
 

Author Comment

by:kidgenius2002
ID: 8154565
also, nothing would be run in the module.  in order to work, the variables need to be declared in the option explicit portion of the module like below:

public var1 as integer
public var2 as integer

sub somesub()
....
end sub

the thing is, i have nothing to put into that "somesub()" part.  i can't just call a module to be run, i'd have to run a subroutine in that moduel correct?  and running a subroutine, in this case is pointless.   if i'm mistaken, then please tell me how to run a module and declare those variables without running and subroutines within the module.
0
 
LVL 1

Expert Comment

by:JH0401
ID: 8154612
may we see your code???
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8154631
All you have to do is add a module to the project and declare the variables

     public var1 as integer
     public var2 as integer

and that's it now you have access to these variables from anywhere within the program.  You do not need to call anything or put anything else within the module.  Also you would waste alot more memory/time doing it differently
0
 

Author Comment

by:kidgenius2002
ID: 8154647
(form3)
Public cellloc As Integer
Public Sub UserForm_Initialize()
    n = 1
    'find where the cells will start
    x = 10
    splcnt = 0
    speccount = 0
    pincount = 0
    blnkcnt = 0
    Do Until x = ActiveCell.Column
        If x > 13 Then
            splcnt = splcnt + 2
            Else
            If Cells(ActiveCell.Row, x) = "PINS" Then
                pincount = 1 + pincount
                Else
                If Cells(ActiveCell.Row, x) = "SPECS" Then
                    speccount = 1 + speccount
                    Else
                    blnkcnt = blnkcnt + 1
                    End If
                End If
            End If
            x = x + 1
            Loop
    cellloc = speccount * 5 + pincount * 11 + 18 + splcnt + blnkcnt
    'make caption show the correct words
    For x = 1 To 2
        Do Until Mid(Cells(1, ActiveCell.Column), n, 1) = " "
            n = n + 1
            Loop
            n = n + 1
            Next x
    crownrad.Caption = Mid(Cells(1, ActiveCell.Column), 1, n - 10) & Mid(Cells(1, ActiveCell.Column), n, 5) & " Measurements"
    'load data into textboxes
        tbxfacewidth.Value = Cells(ActiveCell.Row, cellloc)
        tbxchamfer.Value = Cells(ActiveCell.Row, cellloc + 1)
    'flip-flop between crowned and straight splines
    If Cells(ActiveCell.Row, ActiveCell.Column) = "CROWNED" Then
        tbxfacewidth.Visible = False
        lblfacewidth.Visible = False
        tbxchamfer.Visible = False
        lblchamfer.Visible = False
        tbxmindrop.Visible = True
        lblmindrop.Visible = True
        tbxgage.Visible = True
        lblgage.Visible = True
        tbxmindrop.Value = Cells(ActiveCell.Row, cellloc)
        tbxgage.Value = Cells(ActiveCell.Row, cellloc + 1)
        End If
End Sub

Private Sub CommandButton1_Click()

    If Cells(ActiveCell.Row, ActiveCell.Column) = "CROWNED" Then
        Cells(ActiveCell.Row, cellloc) = Val(tbxmindrop)
        Cells(ActiveCell.Row, cellloc + 1) = Val(tbxgage)
        Else
        Cells(ActiveCell.Row, cellloc) = Val(tbxfacewidth)
        Cells(ActiveCell.Row, cellloc + 1) = Val(tbxchamfer)
        End If
        If headings = "Engine Pitch" Then
        End If
    form4.Show
    Unload Me
End Sub



(form4's intialize)
Public Sub UserForm_Initialize()
       'make caption show the correct words
    For x = 1 To 2
        Do Until Mid(Cells(1, ActiveCell.Column), n, 1) = " "
            n = n + 1
            Loop
            n = n + 1
            Next x
    crownrad.Caption = Mid(Cells(1, ActiveCell.Column), 1, n - 10) & Mid(Cells(1, ActiveCell.Column), n, 5) & " Measurements"
    'load data into textboxes
        tbxdist.Value = Cells(ActiveCell.Row, cellloc)
        tbxdtol.Value = Cells(ActiveCell.Row, cellloc + 1)
        tbxbore.Value = Cells(ActiveCell.Row, cellloc + 2)
        tbxbtol.Value = Cells(ActiveCell.Row, cellloc + 3)
        tbxshaft.Value = Cells(ActiveCell.Row, cellloc + 4)
        tbxstol.Value = Cells(ActiveCell.Row, cellloc + 5)
End Sub

0
 
LVL 1

Expert Comment

by:JH0401
ID: 8154651
Hornet241 -- Look at the first comment after question was posted.  For some reason kidgenius2002 does not want to use a module.  Hmmm?
0
 

Author Comment

by:kidgenius2002
ID: 8154653
the thing is, this is all in excel, the module doesn't just get run.  i have a subroutine in the change event of a cell.  so that way it loads the form for some user input.  the module never gets seen correct?
0
 

Accepted Solution

by:
DroopyA earned 150 total points
ID: 8154700
  If Cells(ActiveCell.Row, ActiveCell.Column) = "CROWNED" Then
       Cells(ActiveCell.Row, cellloc) = Val(tbxmindrop)
       Cells(ActiveCell.Row, cellloc + 1) = Val(tbxgage)
       Else
       Cells(ActiveCell.Row, cellloc) = Val(tbxfacewidth)
       Cells(ActiveCell.Row, cellloc + 1) = Val(tbxchamfer)
       End If
       If headings = "Engine Pitch" Then
       End If
   form4.Show
   ' NO UNLOAD HERE
   ' UNLOAD GETS CALLED IN FORM4 ACTIVATE
End Sub

Form4_Activate
   Call GetForm3Info()
   Unload Form3
End Sub

Read my above Post!

Peace
-N473
0
 

Author Comment

by:kidgenius2002
ID: 8154728
ya, i saw that droopyA.
i think that'll prolly b the way i go
0
 

Author Comment

by:kidgenius2002
ID: 8154836
it ain't pretty, or slick, but it'll do, thx.
0
 

Expert Comment

by:DroopyA
ID: 8156158
Well, the best way would be a modual and a global variable, or maybe even create a class and create one global class. But you said that was too much. Glad to help.

Peace
-N473
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 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