Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

How to get the information froma a form

Hi all,

I create a form to ask the user to enter their name, How coud I can get that information then close the form. Here is my code, but something wrong

sub main1 ()

Set UserForm = New Inputform
'show the form
UserForm.Show

'display the information captured from the form.
MsgBox username

end sub

/the following code is the code for the OK button of the form
Private Sub OKButton_Click()
'here try to set the value of username (string to the value of the textbox (User) in the form)
username = User.value
End Sub

Thanks,

haoyu
0
haoyux
Asked:
haoyux
1 Solution
 
aelatikCommented:
Are you working with VBA or VB ?
0
 
pkmnfrkCommented:
You need to make username a public variable:

Public username as String
0
 
aelatikCommented:
You ment something like this ?

Private Sub Form_Load()
Dim Username As String
Username = InputBox("Enter your name please...", "Name Input")
MsgBox "You have entered : " & Username, vbOKOnly
Unload Me
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bistricaCommented:
create global variable as:

public gUserName as string
sub main1 ()

Set UserForm = New Inputform
'show the form
UserForm.Show

'display the information captured from the form.
MsgBox gUserName

end sub

/the following code is the code for the OK button of the form
Private Sub OKButton_Click()
'here try to set the value of username (string to the value of the textbox (User) in the form)
gUserName = User.text
End Sub
0
 
haoyuxAuthor Commented:
main1 is the VBA code in excel

Then I create a form to ask the user to enter user name and group number.

I want to the following behavior

1. click a button in the excel

2. run the main1 sub

3. the form appears

4. user enters name and group number

5. user clicks the OK button in the form

6. form disappears

7. return the the main1 sub, I use msgbox to show the user's entering.

Thanks,

haoyu
0
 
pkmnfrkCommented:
Ok, here we go:

This goes under the button:

Private Sub cmdName_Click()
    frmUsername.Show 1           'Show dialog box
    uname = frmUsername.txtName  'Get name
    gnum = frmUsername.txtNumber 'Get number
    Unload frmUsername           'unload dialog box
    MsgBox uname                 'Display info
End Sub

This goes in the Ok button on the dialog:

Private Sub cmdOk_Click()
    Me.Hide
End Sub

That's all there is to it. For the code above, the dialog should have two textboxes named txtName and txtNumber, and one command button named cmdOk. The Excel sheet should have a button named cmdName.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now