Results 1 to 13 of 13
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Perplexed

    Perhaps this is something simple but I can't figure this out.



    I have a form where I have 2 buttons. I want both buttons to open the same form but pass on a variable with a different number depending on which button is pressed. The variable is not being passed, so my first test was to test for the result of the variable.

    My variable name is tro
    I tried hard coding tro = 1 on one button and tro = 0 on the other button which worked.

    So I started to test as follows.

    I went to the Form_Load() and added
    Dim tro as Long
    tro =1

    I created a button which simply displays MsgBox "The results of tro is" & tro

    But when I press the button, the message box only says

    The results of tro is

    but it doesn't display the 1

    I tried changing to Dim tro as Double and also tried removing the Dim line.

    Any help would be greatful

  2. #2
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Unable to reproduce your issue. Can you upload a portion of your db where you are having this issue for analysis.

  3. #3
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    When you open the database, you can click on the Form Menu. Click on RMA and then click on Test
    limit is 500 k so i had to zip
    Attached Files Attached Files

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by swenger View Post
    I went to the Form_Load() and added
    Dim tro as Long
    tro =1

    I created a button which simply displays MsgBox "The results of tro is" & tro

    But when I press the button, the message box only says

    The results of tro is

    but it doesn't display the 1
    How well do you understand the "Scope of Variables"?
    You have code that probably looks something like
    Code:
    Private Sub Form_Load()
        Dim tro As Long
        tro = 1
    End Sub
    What is happening is that you declared a variable, "tro", in the form load event. This is known as a "Procedure (local) Variable". The variable exists as long as the procedure is running. Once the procedure ends, the variable dies (along with the value).
    So when you click the button (a different procedure), the 1 is not printed because there is nothing TO print - the form load procedure ended.....

    Because you are using two different forms, you would need a PUBLIC (global) variable. This scope of variable is declared in the declarations section of a standard module.
    If you had a standard module named "Module1", to declare a Public (global) variable, the top 3 line would be
    Code:
    Option Compare Database
    Option Explicit
    
    Public tro As Long

    See
    Variable Scope in VBA for MS Access
    https://bytes.com/topic/access/insig...-vba-ms-access

    and

    DECLARING AND SCOPING VARIABLES
    http://cis.poly.edu/~mleung/CS394/f0...declaring.html
    (this one refers to Excel, but has good information.)

    ------------------------------------

    If you want to use the form Load event, I would pass the value using the "OpenArgs" option of the "DoCmd.OpenForm" command.
    To see usage for "OpenArgs":

    - Open Help
    - Set search to "Developer Reference" (click on the down arrow to the right of the search button)
    - search on "OpenArgs" (no quotes).

  5. #5
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks for your help. No I don't have a lot of experience with coding. I understand the concepts and I am able to understand code as I read it but not building from scratch. I didn't realize that a variable from one form is not passed unless it is in the Public which I know have fixed thanks to you.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Great....


    Note that there are a couple of other options:

    - Pass a value using "OpenArgs"
    - Open the 2nd form, then set an unbound control to the value.

  7. #7
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Can I ask something else

    I am trying to automate a report to be sent by PDF. I figured everything besides the filename. Is there a way to generate the filename

    cmp = IIf(AltInfo = True, Company, From)
    Sbj = "RMA " & RMA & " " & cmp
    fle = "RMA " & RMA & " " & cmp & ".pdf"


    DoCmd.SendObject acReport, "RRMAInfo", acFormatPDF, "rma@company.com", , , Sbj, , True

    The code worked but created the filename the same as the Report Name. Is it possible to insert the variable fle to create the file name. I was able to do this with saving it to disk with DoCmd.OutputTo ....

    Thanks
    Sam

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Is it possible to insert the variable fle to create the file name
    Not that I have been able to find.
    The "SendObject" command does just that - it sends an access object so the name is fixed.

  9. #9
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    That's what i have been seeing online. One suggestion was to save the file and then create an email and attach the file. I tried the code given but there was an error. Any Idea why. My thought is that the code was for an earlier version and I am using 2016

    Private Sub Test_Click()
    loc = "T:\RMA"
    Sbj = "RMA " & RMA
    fle = loc & sbj & ".pdf"

    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem

    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
    .Subject = Sbj
    .Recipients.Add "helasmar@logix-works.com"
    .Attachments.Add fle
    .Body = "Need approval please"
    .Display
    End With

    End Sub

    The error is Compile error: User-defined type not defined

    and it highlights this line Dim appOutLook As Outlook.Application

    Thanks,

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The error is Compile error: User-defined type not defined

    and it highlights this line Dim appOutLook As Outlook.Application
    Did you set a reference to the "OUTLOOK OBJECT LIBRARY"??
    IDE/TOOLS/References--- >> Looking for "Microsoft Outlook xx.0 Object Library" (where xx.0 is the version; A2010 is ver 14.0)

  11. #11
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks for all the help.

    Works perfectly now.

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You're welcome

  13. #13
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    you DIM'd tro in the sub: Form_Load()
    that is the ONLY place it has value. Once you leave Form_Load() it vanishes.

    You want a module level variable
    At the top of the form code (in vb) , under OPTION COMPARE DATABASE (if there)
    private tro

    now tro can exist in every sub in the form.
    remove any other DIM statements on tro

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums