sydodman
asked on
Custom Input Box
I have created a form that is essentially an input box. It has a label, a text box and two buttons. I have set it up so it resembles the rest of my application and thus cant use the standard inputbox control. What I am trying to do is create a sub that loads the input box, changes the label (to a new dialogue) and then returns whatever is inputted into the text box as a result.
For example... If the user double clicked on LblInput1 it would launch the new input box and then change the label to a new prompt for the user
If the no button is clicked the input box would unload, if the yes button is clicked whatever had been entered into the inputbox would be set to LblInput1.Caption
If the user double clicked in LblInput2 the same would happen but this time the same form would have to assign the contents of the textbox to this new LblInput2.Caption
Thanks in advance
Sy
ASKER
I think I understand the principle of the code but I am fairly new to visual basic and am not really sure how to make what you have given VB friendly...
For exmaple I cant get the docmd.openform to work, the set form returns a type mismatch and the call forms doesnt work either
Sorry
For exmaple I cant get the docmd.openform to work, the set form returns a type mismatch and the call forms doesnt work either
Sorry
This is almost the same:
In your inputbox form add this code:
-------------------------- ---------- ---------- -------
Private vRetVal As Variant
Public Function ShowWindow(strPrompt As String) As Variant
Label1.Caption = strPrompt
Me.Show vbModal
ShowWindow = vRetVal
End Function
Private Sub cmdCancel_Click()
'This is the evento for the cancel button
vRetVal = False
Me.Hide
End Sub
Private Sub cmdOK_Click()
'This is the event for the OK button
vRetVal = Text1.Text
Me.Hide
End Sub
Now you can use ShowWindow every time you want to call your form, like this:
-------------------------- ---------- ---------- -------
Private Sub Label1_DblClick()
Dim vRet As Variant
vRet = Form1.ShowWindow("Write something")
If Not vRet = False Then
'OK, Change caption
Label1.Caption = vRet
Else
'Cancel was press, Do nothing
End If
End Sub
In your inputbox form add this code:
--------------------------
Private vRetVal As Variant
Public Function ShowWindow(strPrompt As String) As Variant
Label1.Caption = strPrompt
Me.Show vbModal
ShowWindow = vRetVal
End Function
Private Sub cmdCancel_Click()
'This is the evento for the cancel button
vRetVal = False
Me.Hide
End Sub
Private Sub cmdOK_Click()
'This is the event for the OK button
vRetVal = Text1.Text
Me.Hide
End Sub
Now you can use ShowWindow every time you want to call your form, like this:
--------------------------
Private Sub Label1_DblClick()
Dim vRet As Variant
vRet = Form1.ShowWindow("Write something")
If Not vRet = False Then
'OK, Change caption
Label1.Caption = vRet
Else
'Cancel was press, Do nothing
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can do something like...
' --------------------
' Form1
' --------------------
Option Explicit
Private Sub Command1_Click()
Dim answer As String
Dim result As Boolean
answer = Form2.ShowDialog("What is your name?", result)
If result Then
Label1.Caption = answer
End If
End Sub
' --------------------
' Form2
' --------------------
Option Explicit
Private answer As String
Private cancelled As Boolean
Public Function ShowDialog(ByVal prompt As String, ByRef result As Boolean) As String
answer = ""
Form2.Caption = prompt
Form2.Show vbModal
result = Not cancelled
ShowDialog = answer
End Function
Private Sub cmdOk_Click()
answer = Text1.Text
cancelled = False
Me.Hide
End Sub
Private Sub cmdCancel_Click()
cancelled = True
Me.Hide
End Sub
' --------------------
' Form1
' --------------------
Option Explicit
Private Sub Command1_Click()
Dim answer As String
Dim result As Boolean
answer = Form2.ShowDialog("What is your name?", result)
If result Then
Label1.Caption = answer
End If
End Sub
' --------------------
' Form2
' --------------------
Option Explicit
Private answer As String
Private cancelled As Boolean
Public Function ShowDialog(ByVal prompt As String, ByRef result As Boolean) As String
answer = ""
Form2.Caption = prompt
Form2.Show vbModal
result = Not cancelled
ShowDialog = answer
End Function
Private Sub cmdOk_Click()
answer = Text1.Text
cancelled = False
Me.Hide
End Sub
Private Sub cmdCancel_Click()
cancelled = True
Me.Hide
End Sub
Sorry rettiseert...didn't refresh.
My code is basically the same as yours. =)
My code is basically the same as yours. =)
ASKER
Thanks very much,
A copy and paste answer!!! Just the kind I like!!!
Thanks to everyone else also
Sy
A copy and paste answer!!! Just the kind I like!!!
Thanks to everyone else also
Sy
(from anywhere)
Global sReturnValue as String '<--- Play with this to suit your needs..
(from the calling code)
Dim frm as Form
DoCmd.OpenForm "fdlg_your_dialog_form", acNormal
Set frm = Forms("fdlg_your_dialog_fo
sReturnValue = ""
Call forms.fdlg_your_dialog_for
(from within your custom inbut box form)
Public sub fn_update(sLabel as String)
If Len(sLabel) > 0 then
Me.SomeLabelControl.Captio
else
'Handle the instance of code opening this form with no label)
end if
end sub
Private Sub SomeButton_Click()
If Len(SomeTextBoxValue) > 0 then
sReturnValue = SomeTextBoxValue
else
'Handle the instance of user clicking 'Ok' button without adding text)
end if
UnLoad Me
End Sub
Hope this helps.
-Jim