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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to get info entered in a text box by user 1

Status
Not open for further replies.

mlstewart

Instructor
Aug 20, 2004
58
US
I've got a text box named ActivationCodeBox on a form where the user will enter a string of characters (numbers or letter) and then click the Enter button. Currently the text box is Unbound. I want the string of characters entered by the user to be compared to a variable called EntryCode. If they are equal then I want the form to close but if they aren't equal then I want a message to display. This is what I have:



Private Sub EnterActivationCodeCMD_Click()

Dim x As Integer
Dim EntryCode as String

EntryCode = ActivationCodeBox.Text

'The EntryCode that will be entered in the text box is
'actually the computer name where each letter/number has
'been incremented by one

For x = 1 To Len(EntryCode)
Mid(EntryCode, x, 1) = Chr(Asc(Mid(EntryCode, x, 1)) +1)
If Mid(EntryCode, x, 1) = "[" Then
Mid(EntryCode, x, 1) = "A"
ElseIf Mid(EntryCode, x, 1) = ":" Then
Mid(EntryCode, x, 1) = "1"
End If

Next

If EntryCode = Environ("ComputerName") Then
DoCmd.Close
ElseIf EntryCode <> Environ("ComputerName") Then
MsgBox "You did not enter a valid Activation
Code."

End If

End Sub


The problem I am having is when I enter something in the text box and click on the Enter button, I am getting a run time error '2185' that says "You can't reference a property or method for a control unless the control has the focus." When I click Debug, the line of code that is highlighted is "EntryCode = ActivationCodeBox.Text"


Can someone please help me.

Thanks,
ML
 
You can't reference the Text property, as it says, when the control doesn't have the focus. However, you can reference the default property (which is named Value) by omitting the ".Text" part. So in other words, you need:
EntryCode = ActivationCodeBox
(Actually, most of us would write it:
Me.EntryCode = Me.ActivationCodeBox
but I think your syntax also should work.)

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks RickSpr. That worked.

I've still got one problem. If I click the Enter button without entering anything in the text box, then I get a run time error '94': Invalid Use of Null. I've tried several things but can't seem to fix it.

Thanks for your help,
ML
 
to "cheat" away from NULL,
do a
Code:
variableName = " " & variableName

if you have problems with focus, simply use:
Code:
object.setfocus
before handling with the object..

instead of "cheating" NULL, I would do an:
IF variableName <> NULL THEN
'foo
ELSE
'bar
END IF
 
try:
Code:
Me.EntryCode = " " & Me.ActivationCodeBox

if you have problems with focus:
Code:
object.setfocus

you can also do
Code:
IF Me.ActivationCodeBox <> NULL THEN
  ' your code
ELSE
  msgbox("you need input, noob!")
END IF

ps.. might be some errors here.. I'm tired and want to go home from wrk.

good luck!
 
Thanks DaButcher!

I tried several things with the If/Else code you posted but didn't have any luck. I then tried the object.setfocus you suggested like this:


ActivationCodeBox.SetFocus

ActivationCode = ActivationCodeBox.Text


and that works great. Thanks so much!!!
 
A simpler and, in general, better solution:
EntryCode = Nz(Me.ActivationCodeBox, "")

The Nz() function converts Null to whatever you specify as its second argument--in this example, an empty string.

DaButcher's suggestion of " " & ActivationCodeBox sets EntryCode to a 1-character string containing a space. If it works for you in this situation, fine, but in general equating a Null to a space character will give you more problems. With DaButcher's other suggestion you have to move the focus to the control, which can be confusing to the user. You don't have to do that if you use Nz().

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks RickSpr!
That works great. I'm sure I will be using that function again in the near future.

ML
 
RickSpr:
yes, you have a point there..

however:

Code:
EntryCode = Nz(Me.ActivationCodeBox, "")

and:

Code:
EntryCode = Me.ActivationCodeBox & ""

Will do the same, wont it?
I know I did a glitch, when doing " " instead of "".
 
DaButcher: Yes, those are quivalent. Access converts Null to an empty string when it's involved in concatenation. I often use that method myself, but in this case I suggested Nz() because it's works with all data types, and a lot of people don't realize it exists.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top