Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    3

    Unanswered: updating TextBox from subform TextBox

    Hello all;

    I've utilized the code posted by Izyrider several years ago. The intent is to replace all calendar controls in my multi-tab data entry form with textboxes updated via the popUserDate form that is called from the click event of the originating textbox as per below...

    Private Sub TxtDateOccur_Click()
    On Error GoTo Err_TxtDateOccur_Click

    ' calling form MUST use the following syntax:
    Dim TargetTextBox As TextBox 'prepare textbox object
    Set TargetTextBox = Me.TxtDateOccur 'set the target textbox
    DoCmd.OpenForm "popUserDate" 'open the form
    Forms!popUserDate.popUserDateSetup TargetTextBox, "Select Date", Now() 'call the setup routine
    Dim PUDtarget As TextBox 'the TargetTextBox in the calling form
    Dim initDate As Date

    Exit_TxtDateOccur_Click:
    Exit Sub

    Err_TxtDateOccur_Click:
    MsgBox Err.Description
    Resume Exit_TxtDateOccur_Click

    End Sub


    The popUserDate form is a simple form with a calendar control and three command buttons. The code for the popUserDate is as follows...

    Option Compare Database

    Public Sub popUserDateSetup(theTarget As TextBox, theCaption As String, theDefault As Date)
    Set PUDtarget = theTarget 'keep a local copy of the target textbox object
    Me.Caption = theCaption 'set the form caption
    If IsDate(theDefault) Then
    initDate = theDefault 'save the initial value for a potential user-reset
    Else
    initDate = Now() 'if incoming date is bad, use today's date
    End If
    theCal = initDate 'set the calendar to the initial value
    End Sub

    Private Sub butCancel_Click() 'cancel: caller textbox is NOT changed
    DoCmd.Close acForm, "popUserDate"
    End Sub

    Private Sub butClear_Click()
    'null the source date
    PUDtarget = ""
    DoCmd.Close acForm, "popUserDate"
    End Sub

    Private Sub butOK_Click() 'OK: update caller texbox and close popUserDate
    PUDtarget = theCal
    DoCmd.Close acForm, "popUserDate"
    End Sub


    Everything is working fine except that the value of PUDtarget is not being passed back to the originating textbox. I was able to successfully specify the actual field to update but this defeats the purpose as I want to use this utility form to update many date textboxes throughout the application so I have to make the object variables work properly.

    As you can probably tell, I'm relatively new at this so any suggestions would be greatly appreciated.

    Thanks,

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    The TargetTextBox variable is deallocated at the end of TxtDateOccur_Click and the variable PUDtarget is private to TxtDateOccur_Click as it is declared inside this sub (itself declared as private).
    Moreover popUserDate is not a subform of the main form.

  3. #3
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    3
    Thanks for the input Sinndho.

    I've changed the TxtDateOccur_Click event to Public and just for fun added in a msgbox line in the butOK sub of the popUserDate to verify that the PUDtarget variable is collecting the data from the calender control and it is. Still cannot get the PUDtarget variable to pass it's value to the TargetTextBox variable though. This is the variable that I would like to use many times throughout the application to collect the date value from the popUserDate form. My apologies on the subform wording...the popUserDate form is simply a small utility form completely unrelated to the form that holds the TxtDateOccur_Click sub. Any other ideas?
    Last edited by mlcoderre; 03-11-09 at 20:01.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    This only works because you do not have the Option Explicit set on in your modules.
    You do not declare PUDtarget anywhere in the PopUserDate form, so it is implicitly declared as a local variant in each procedure when you refer to it. When exiting those procedures (popUserDateSetup, butCancel_Click, butClear_Click and butOK_Click) is implicitly set to nothing and falls out of scope.

    Add
    Code:
    Option Explicit
    in the declaration section of the PopUserDate module and you'll see that you cannot compile it anymore because PUDtarget is never declared.

    see: Scope of variables in Visual Basic for Applications

    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •