+ Reply to Thread
Results 1 to 12 of 12

Refer to textbox on another UserForm via variable

  1. #1
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Refer to textbox on another UserForm via variable

    64bit 2010, so I'm using this calendar control for a date picker: https://sites.google.com/site/e90e50...ss#TOC-UPDATES

    Right now, I'm planning on being able to put dates into text boxes on UserForms - specifically, three text boxes (total) on two different forms. For testing purposes, I did go ahead and add two textboxes to the calendar control previously linked: one is txtUserForm, the other is txtControl. The idea is to pass the name of the UserForm and the textbox control into these, and then use those values to pass the selected date back to the correct textbox on the correct form. Seems simple enough, but I just can't get it sorted. Sidenote: I found that referencing the index of the textbox control seems to perhaps be a better option. That is what the below is doing.

    Here's the code I have. Any help or insight is greatly appreciated.
    Please Login or Register  to view this content.
    Please remember to hit the Add Reputation for any member that has been helpful.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    Instead of showing your non-working code, a better description of what you are actually trying to do would be useful.

    There are all sorts of ways to pass information: public variables, parameters in macro or function definitions, writing the value to the sheet, registry, or custom document property, etc., etc.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Refer to textbox on another UserForm via variable

    OK.

    I have two UserForms that require date entries. Hence the use of the Calendar Control previously linked. The first UserForm is AddProject, with a single date field (textbox) of txtDate. Entering txtDate activates the Calendar Control. Same thing with my other UserForm, AddTask, which has two textboxes for dates: txtSoftDate and txtHardDate.

    The idea, then, is to select a date on the Calendar Control and return that date to the appropriate textbox that is calling/activating said Calendar Control. Hence I want to...refer to textbox on another UserForm via variable.

    I can do it by hardcoding the names in. I thought about a simple Select Case, because it will work and it's easy enough to do. But I figure there has to be a way to refer to the specific UserForm and the specific textbox via variables, and pass the selected date from the Calendar Control into those specific forms/textboxes.

    Hopefully this clarifies things, and I do appreciate your time.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    I think your calendar control is a userform with controls on it. If so, in the codemodule of your userform AddProject, use code like this, so that the date chooser-from appears when the date filed is activated (change UserFormCalendar to whatever you are calling it, as well as TextBox1)

    Private Sub TextDate_Enter()
    Load UserFormCalendar
    UserFormCalendar.Show
    Me.TextDate.Text = UserFormCalendar.TextBox1.Text
    Unload UserFormCalendar
    End Sub

    and for the UserFormCalendar, don't unload it when the OK or Enter or whatever button is clicked - just hide it so control goes back to your other userform but the value is still available to be read:

    Private Sub CommandButton1_Click()
    Me.Hide
    End Sub
    Last edited by Bernie Deitrick; 04-02-2015 at 01:42 PM.

  5. #5
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Refer to textbox on another UserForm via variable

    OK, so I'm calling the date picker like this:
    Please Login or Register  to view this content.
    I added the: me.txtdate=calendar1.value, as suggested.

    On the Calendar form, it already had me.hide

    Adjusting the code as you suggested doesn't seem to work - it tries to retrieve the date value before a date is selected on the new form. I need to pass the date value only after it is selected.

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    Your line:

    me.txtdate=calendar1.value

    needs fully qualified objects:

    me.txtdate.text = DatePickerForm.calendar1.value
    or text...
    me.txtdate.text = DatePickerForm.calendar1.Text

    me.txtdate=calendar1.value would work if calendar1 was on the same userform as txtdate

    If you need further help, it would be better is you posted a sanitized workbook with your userforms.

  7. #7
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Refer to textbox on another UserForm via variable

    Unfortunately, the firewall blocks outbound files to forums. I have, however, managed to save a copy to Google Drive. https://drive.google.com/file/d/0B3K...ew?usp=sharing

    The DatePicker holds the Calendar Control downloaded from the link in my first post. AddProject launches said control upon entry of the date textbox.

    AddTask will have similar behavior, with two date fields, though that has not been coded yet.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    The code you are using writes the date to a range, which you need to set in code. Try this version - click the button, and add tasks with the two dates - the data is not written to the sheet yet, but the dates are selectable.

    My Task Thingy.xlsm

  9. #9
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Refer to textbox on another UserForm via variable

    When I click the button on the sheet, and try to enter the Date textbox, it throws the error: Method or data member not found and highlights, on txtDate_Enter(): Set DatePickerForm.Target = Target

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    I did not see that date entry box....My Task Thingy.xlsm

  11. #11
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Refer to textbox on another UserForm via variable

    You know, I came and asked for help because I figured it was one of two things: something more complex and way over my head, or something fairly simple that I was making complex. I'm so glad to see it's the latter.

    Thank you, sir! Rep added.

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Refer to textbox on another UserForm via variable

    You're welcome, and thanks for letting us know it worked out for you, and for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Trying to refer to a textbox in a userform from a module in a for next loop
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2015, 03:24 PM
  2. [SOLVED] Refer to TextBox useing Variable?
    By TomToms in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-09-2014, 04:05 PM
  3. Refer sheet1 with textbox to a variable
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2014, 12:45 PM
  4. VBA, Userform, Textbox: Refer to TextBox Name as a Variable
    By Brianandstewie in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 07-09-2013, 05:49 PM
  5. Excel VBA: Can I refer to a textbox as a variable?
    By BigBas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2013, 06:18 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1