• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1806
  • Last Modified:

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
0
sydodman
Asked:
sydodman
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The below is Access code that does the same thing, you'll have to make it VB-friendly:

(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_form")

sReturnValue  = ""
Call forms.fdlg_your_dialog_form.fn_update("Your Label Goes Here")

(from within your custom inbut box form)

Public sub fn_update(sLabel as String)

If Len(sLabel) > 0 then
   Me.SomeLabelControl.Caption = sLabel
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

0
 
sydodmanAuthor Commented:
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
0
 
rettiseertCommented:
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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
rettiseertCommented:
I forgot, add this line to the showwindow function:

Public Function ShowWindow(strPrompt As String) As Variant
    vRetVal = False '<----------------- add this line
    Label1.Caption = strPrompt
    Me.Show vbModal
    ShowWindow = vRetVal
End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry rettiseert...didn't refresh.

My code is basically the same as yours.   =)
0
 
sydodmanAuthor Commented:
Thanks very much,

A copy and paste answer!!!  Just the kind I like!!!

Thanks to everyone else also

Sy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now