If TypeName(UserVal) <> "Boolean" Then
Range("H107") = UserVal
Else
Exit Sub
End If
Sorry, I missed an End If:
Code:If TypeName(UserVal) <> "Boolean" Then Range("H107") = UserVal Else Exit Sub End If
Dato = InputBox("Dato for måling" & Chr(13) & Chr(13) & "Skrives i formatet" & Chr(13) & "01.02.2011 for 1.feb.2011", , Date)
If Dato = False Then Exit Sub
If Dato = "" Then Exit Sub
If the user clicks Cancel, the InputBox function returns a zero-length string (""). S try:
Code:If Dato = "" Then Exit Sub
Note that Excel's InputBox returns False if the user clicks Cancel. For that you would use Application.InputBox.
There are 2 versions of InputBox in VBA.
The InputBox Function is called without an object qualifiier and returns the contents of the text box or a zero-length string ("") if the user clicks Cancel.
The InputBox Method is a member of the Application object, so it is called by using Application.InputBox. It returns the contents of the text box or False if the user clicks Cancel. It is more versatile than the InputBox Function because it has a Type argument which specifies the return data type.
So Nimrod's original code (InputBox Function) is OK (provided that you don't want to do something if the user enters nothing and click OK).
In Excel VBA, Application refers to the Excel; so it appears (I don't use Access) that Access doesn't provide the method.
Can you not just use the InputBox function?
Sub x()
Dim sInp As String
sInp = InputBox("Enter something")
If StrPtr(sInp) = 0 Then
MsgBox "User pressed Cancel"
ElseIf Len(sInp) Then
MsgBox "User entered """ & sInp & """ and pressed OK"
Else
MsgBox "User entered nothing and pressed OK"
End If
End Sub