Passing a Variable to a Popup Form.

Steve R.

Retired
Local time
Today, 06:11
Joined
Jul 5, 2006
Messages
5,652
My understanding of passing a variable directly from to a popup form is incomplete. The following code works, but requires an "unnecessary" variable assignment.

The popup is called with the following code. However, because I have been unable to pass stUnderLyingfrm and stRecordSource to the popup form, I am using a subroutine. Rather than have two popup forms, I am trying to use one, which is why I am trying to pass the record source. All these variables have been defined as PUBLIC in the declaration section.

Code:
Public Sub Command72_Click()
    intBookId = Me.BOOKID
    stUnderLyingfrm = Me.Name
    stRecordSource = "Qalpha"
    Rem stDocName = "IMAGEADDFRM"
    Rem stLinkCriteria = "BOOKID= " & intBookId
    Rem DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria, ,acDialog
    Call IMAGEADD(intBookId, stUnderLyingfrm, stRecordSource)    
    End Sub

Below is the subroutine. For some reason, I must pass the variable intBookId to intBook01id for the program to work, but this should not be necessary which means that I am not understanding something.

Code:
Public Sub IMAGEADD(intBookId As Integer, stUnderLyingfrm As String, stRecordSource As String)
    intBook01id = intBookId
    stUnderLying01frm = stUnderLyingfrm
    stRecordSource01 = stRecordSource
    stDocName = "IMAGEADDFRM"
    stLinkCriteria = "BOOKID= " & intBookId
    DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria, , acDialog
End Sub

Partial sample from the popup form to show how stRecordSource01 is used. Ideally, I should be able to reference stRecordSource directly, but I must be missing something somewhere.
Code:
Public Sub Form_Open(Cancel As Integer)
    stDocName = Me.Name
    Forms(stDocName).RecordSource = stRecordSource01
    Forms(stDocName).AllowAdditions = False   
End Sub
 
Not sure what you're trying to do, but you can pass parameters to a popup form using the OpenArgs parameter of the DoCmd.OpenForm method. You can pass in any number of parameters if use a delimited string which you process in the form after the form opens. Consider....

Code:
Private Sub SomeRoutine()
  dim args as string

  args = "1~Some Data~Other Data~12345~Hello"
  docmd.openform "yourPopup", openargs:=args
End Sub

Private Sub Form_Open(Cancel as integer)
' This is the open event handler of your popup
  dim var
  dim i as integer

  var = split(me.openargs, "~")
  for i = 0 to ubound(var)
    'here's your data available as members of an array
    debug.print var(i)
  next i
end sub
 
Thanks for the sample, it is clearer than the ACCESS help sample. I will test it out.

I figured out that I had too many PUBLIC statments. Once I got rid of the duplicate ones, the variables passed as expected. In retrospect this is "obvious", but I guess this was one of those excercises where you learn through experience.

I have two forms, that in theory could be reduced to one universal form by having VBA variables define the RECORDSOURCE and the relevent key field. For now, I am going to revert to using two forms and eliminate the subroutine to get the database more operational and return to combining the forms later.
 

Users who are viewing this thread

Back
Top Bottom