0% found this document useful (0 votes)
14 views

Prog

The document discusses using input boxes and combo boxes in VBA. It provides code examples to: 1) Use an input box to prompt the user to enter a number, check if it is numeric, and write it to cell A1 if valid. 2) Use a combo box to select a roll number, retrieve the matching record from a table, and populate text boxes with the name, address, and age fields. 3) The input box example uses If statements and a While loop to repeatedly display the input box until a valid number is entered or the user cancels.

Uploaded by

Muthu Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Prog

The document discusses using input boxes and combo boxes in VBA. It provides code examples to: 1) Use an input box to prompt the user to enter a number, check if it is numeric, and write it to cell A1 if valid. 2) Use a combo box to select a roll number, retrieve the matching record from a table, and populate text boxes with the name, address, and age fields. 3) The input box example uses If statements and a While loop to repeatedly display the input box until a valid number is entered or the user cancels.

Uploaded by

Muthu Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub Using_InputBox_Method()

Dim Response As Integer

' Run the Input Box.


Response = Application.InputBox("Enter a number.", _
"Number Entry", , 250, 75, "", , 1)

' Check to see if Cancel was pressed.


If Response <> False Then

' If not, write the number to the first cell in the first sheet.
Worksheets(1).Range("a1").Value = Response

End If

End Sub

The Input Box Function

In the example below, a series of If statements is used to check the entry. The InputBox is inside
a While loop to allow it to be re-shown if an error occurs. If all the conditions are true, the
entered number is written to cell A1 on the first worksheet and the loop is ended.
Sub Using_InputBox_Function()
Dim Show_Box As Boolean
Dim Response As Variant

' Set the Show_Dialog variable to True.


Show_Box = True

' Begin While loop.


While Show_Box = True

' Show the input box.


Response = InputBox("Enter a number.", _
"Number Entry", , 250, 75)

' See if Cancel was pressed.


If Response = "" Then

' If Cancel was pressed,


' break out of the loop.
Show_Box = False
Else
' Test Entry to find out if it is numeric.
If IsNumeric(Response) = True Then
' Write the number to the first
' cell in the first sheet in the active
' workbook.
Worksheets(1).Range("a1").Value = Response
Show_Box = False
Else
' If the entry was wrong, show an error message.
MsgBox "Please Enter Numbers Only"
End If
End If
' End the While loop.
Wend
End Sub
Combo box
DIm RST As New ADODB.RecordSet
Dim sSQL As String

sSQL = "Select * From MyTable Where RollNo = " & Val(Combo1.Text)


RST.Open sSQL, Conn
If Not RSt.EOF Then
txtName.Text = RST("Name")
txtAdd.Text = RST("Addr")
txtAge.Text = RST("Age")
Else
txtName.Text = ""
txtAdd.Text = ""
txtAge.Text = ""
End If
RST.Close
Set RST=Nothing

You might also like