Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables in Macros 1

Status
Not open for further replies.

notapplicable66

Technical User
Dec 5, 2005
2
US
I'm trying to figure out how to pop-up a dialog box that allows the user to input a string of text and then store it in a variable. This will prevent having to type the same thing over and over and be much more efficent. Any help is greatly appreciated.
 
MyVar = InputBox("Please enter your text here.")

MyVar is now a variable with the text the user entered.

calculus

Check here for a few other helpful starter functions:
I'm ready for Extra Basic... now what? faq99-4087
 
Thanks, now on the input box is there any way for the user to input more than one field in a single box? I guess a form would be a better way of refering to it.
 
In EB a form is called a dialog box. If you need to capture a lot of user info, I'd suggest you use VBA from Excel as this will give you better control over forms. In EB, dialog boxes are extremely clumsy and often times a user will "lose" the box under another application. While systematically this isn't an issue, to the user it is extremely confusing.

Another way of doing things would be to present a set of inputboxes if you don't have to collect too much info from the user.

calculus

Here are my other FAQs:
How do I use VB(A) to manipulate attachmate (6.5+)? faq99-4069
I'm ready for Extra Basic... now what? faq99-4087
How do I get data to Excel? faq99-4068

 
Give this a try mein


Declare Function FileDlgFunction(identifier$, action, suppvalue)
Dim UserInput(4) as string

Sub Main()

Begin Dialog userdialog 130, 88, "INPUT INFO", .FileDlgFunction

OkButton 5, 70, 35, 14
CancelButton 75, 70, 30, 14
TextBox 5, 5, 120, 13, .TextBox1
TextBox 5, 20, 120, 13, .TextBox2
TextBox 5, 35, 120, 13, .TextBox3
TextBox 5, 50, 120, 13, .TextBox4
End Dialog

Dim mydialog as UserDialog
On Error Resume Next
Dialog mydialog
If Err=102 then
MsgBox "Dialog box canceled."
End If

for x = 1 to 4
msgbox "UserInput "+str(x)+" = "+ UserInput(x)
next
End Sub

Function FileDlgFunction(identifier$, action, suppvalue)
Select Case action
Case 1 'dialog box initialized
Case 2 'button or control value changed
Case 3 'text or combo box changed
UserInput(1) = DlgText$(2)
UserInput(2) = DlgText$(3)
UserInput(3) = DlgText$(4)
UserInput(4) = DlgText$(5)
Case 4 'control focus changed
Case 5 'idle
DoEvents
End Select
End Function
 
Small improvement to this code:

Code:
Declare Function FileDlgFunction(identifier$, action, suppvalue)
Dim UserInput(4) as string

Sub Main()

Begin Dialog userdialog 130, 88, "INPUT INFO", .FileDlgFunction
   TextBox  5, 5, 120, 13, .[b]TextBox1[/b]
   TextBox  5, 20, 120, 13, .[b]TextBox2[/b]
   TextBox  5, 35, 120, 13, .[b]TextBox3[/b]
   TextBox  5, 50, 120, 13, .[b]TextBox4[/b]
   OkButton  5, 70, 35, 14
   CancelButton  75, 70, 30, 14
End Dialog

Dim mydialog as UserDialog
On Error Resume Next
Dialog mydialog
If Err=102 then
    MsgBox "Dialog box canceled."
End If

for x = 1 to 4
    msgbox "UserInput "+str(x)+" = "+ UserInput(x)
next
End Sub

Function FileDlgFunction(identifier$, action, suppvalue)
   Select Case action
     Case 1 'dialog box initialized
     Case 2 'button or control value changed
     Case 3 'text or combo box changed
         UserInput(1) = DlgText$("[b]TextBox1[/b]")
         UserInput(2) = DlgText$("[b]TextBox2[/b]")
         UserInput(3) = DlgText$("[b]TextBox3[/b]")
         UserInput(4) = DlgText$("[b]TextBox4[/b]")
     Case 4 'control focus changed
     Case 5 'idle
         DoEvents
  End Select
End Function

If you have the DlgText function refer to the record name (.TextBox1) rather than the item number (2) in the dialog box, it allows you to easily add and remove fields in the dialog box. Also, you generally want the buttons to be after the fill-ins. This mostly affects the tab order, because in Case 1 you can do a DlgFocus "TextBox1" to indicate where the cursor should position when the dialog box opens.
 
Another Idea but it needs some work.


Code:
Option Explicit

' Following declarations are used in dialog processing
const exActInitialize = 1
const exActButtonClick = 2
const exActTextChange = 3
const exActFocusChange = 4
const exActDialogIdle = 5

Dim mltTextArray() as String

Function DlgProc(strID as string,intAct as integer,lngVal as long) as Integer
  Select Case intAct
    Case exActInitialize
      'set caption of OK button to "Done"
      DlgText "btnDone", "Done"

      ' clear out working text array
      Erase mltTextArray

      Redim mltTextArray(0)
      DlgListBoxArray "lstBox1", mltTextArray

      DlgFocus "txtBox1"

  Case exActButtonClick
    mainDlgProc = true ' keep dialog on display

      Select Case strID
        Case "btnDone"
          mainDlgProc = false ' dismiss dialog
        Case "btnCR"
          mltTextArray(UBound(mltTextArray)) = DlgText("txtBox1")
          Redim Preserve mltTextArray(UBound(mltTextArray) + 1)
          DlgText "txtBox1", ""
          DlgListBoxArray "lstBox1", mltTextArray
          DlgFocus "txtBox1"
        End Select
  End Select

End Function

Sub Main
Begin Dialog newdlg 186, 92, .DlgProc
  TextBox 16, 8, 99, 12, .txtBox1
  OKButton 16, 2, 1, 1, .btnCR
  PushButton 130, 6, 50, 14, "OK", .btnDone
  ListBox 16, 21, 99, 54, "Line 1" & chr(9) & "Line 2", .lstBox1
End Dialog

dim dlg as newdlg

Dialog dlg

End Sub

needs more work but another possibility
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top