Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Unanswered: VBA - Dinamically use forms

    Hello.

    I'm building a form for users to fill with information.
    Several fields are dates.

    What i'm trying to do is to use the activeX calendar to pick a date.

    I'm trying to do this dinamically using 2 global variables, one with the form name that is in focus, and another with the control name where i want to put the date.
    I'm having some problems in this. Can anyone help me?

    The code i'm using when user choses a date is the following:

    Private Sub ChooseDate_Click()
    Dim InitialForm As Object

    Set InitialForm = NameOfForm 'NameOf Form is the name of the form that invoked the calendar

    dateChosen = CalendarCTRL.Value

    Forms!NameOfForm!NameOfControl.Value = dateChosen 'NameOfControl is the control name where i want to put the date chosen.


    DoCmd.Close

    End Sub

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Try

    Forms(NameOfForm).Controls(NameOfControl).Value
    Paul

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I won't go into the reasons why the better choice would be to use a Form-based, non-version dependent calendar, rather than an ActiveX calendar, but just address how to use a single calendar for multiple date fields, on a Form.

    A single line of code will do the job:

    Code:
    Private Sub YourCalendarName_AfterUpdate()
     Screen.PreviousControl = YourCalendarName
    End Sub

    You simply Tab/Click into the Control for the date field in question, then click on the calendar.

    Actually, I used to expand on this by simply:
    1. Setting the Calendar's Visible Property to False, by Default
    2. Set the Calendar's Visible to True when a Date Field gets Focus
    3. Assign the Value
    4. Move Focus away from the calendar, by setting it on another Control
    5. Setting the Calendar's Visible Property to False, again


    So that would be, setting the Default Visible Property to False, in the Properties Pane.

    Code:
    Private Sub YourCalendarName_AfterUpdate()
     Screen.PreviousControl = YourCalendarName
     AnyOtherFieldName.SetFocus
     YourCalendarName.Visible = False
    End Sub

    Code:
    Private Sub Date1Field_GotFocus()
     YourCalendarName.Visible = True
    End Sub


    Replacing the various Control names with actual names from your Form.
    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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