PDA

View Full Version : Changing docvariable result depending on input in textbox



sessionone
04-28-2009, 02:27 PM
Hi all,

I'm new to VBA, so please be patient with me as I'm not even sure if I can explain properly what I want my form to do.

I have a userform in Word with a few texboxes, combo boxes, and radio buttons. I am using docvariables in the document to record changes in controls.

Now I want a few of the docvariables to display different values depending on the input in the same textbox.

So, I have a textbox called "code". when the combination is entered in the textbox, the docvariable varCode displays the code on the document. There is another docvariable varQuant related to varCode, which must change depending on the code. So, if the code that's being entered begins with a "3", then the varQuant docvariable displays one value. If the code starts with a "4", then again a different value appears in the varQuant.

Hope this is clear enough, although it sounds like waffling :)

Thanks for your help

Sessionone

Tommy
04-29-2009, 09:08 AM
Hi sessionone,

Where every you set the value of varCode add the below.

Select Case Left(varCode, 1)
Case "3"
varQuant = AValue
Case "4"
varQuant = AnotherValue
Case Else
varQuant = OOPS
End Select


HTH

sessionone
04-29-2009, 03:08 PM
Thanks for this as well, mate.

But either I will have to do the whole userform again or repair the bloody template somehow. Not sure what happened.....

Thanks again,

sessionone

Tommy
04-30-2009, 01:11 PM
Let us know how it goes, if you need more help we are here waiting :beerchug:

sessionone
05-09-2009, 11:11 AM
Hi again,

So finally I got my hands onto this, but unfortunatelly your suggestion didn't work. All I get in where I have my varQuant in the document is "oops":).

What I'm trying to do is to apply a grammar rule. The code I was talking about consists of 11 numbers. So depending on wether the code starts with "3" or "4" the outcome of varQuant will have different words for singular ("3") and plural ("4"). The whole thing is not in english, but in one of the oldest and very complicated languages, where grammar with absolutely brutal :)

Thanks for further suggestions,

sessionone

Tommy
05-11-2009, 01:01 PM
Can you post what you have with all confidential material removed?
If you comment the code in English (deep south, USA :devil2: ) I will be able to figure out what is wrong.

sessionone
05-11-2009, 01:48 PM
cheers mate,

I've got what I wanted using if statements. Not sure if it's very efficient, but it works. But I do have many many more questions for other threads :)

thanks for your time anyway

sessionone

Tommy
05-11-2009, 02:00 PM
Can you post your solution?

sessionone
05-11-2009, 02:47 PM
hmm.....but don't laugh:)

So the code must be 11 numeric characters only, and it must begin either with "3" or "4":
Private Sub txtCode_AfterUpdate()

If Left(txtCode, 1) > 4 Then MsgBox "Error"
If Left(txtCode, 1) < 3 Then MsgBox "Error"
If Len(txtCode.Value) < 11 Then MsgBox "Error"

End If

End Sub
Now for the docvariables:

With ActiveDocument

If Left(txtCode, 1) = "3" Then .Variables("varQuant").Value = "This"
If Left(txtCode, 1) = "4" Then .Variables("varQuant").Value = "That"
.Range.Fields.Update

End With
Hope this is clear enough.

I was also trying to restrict the entry into txtCode to numbers only, but after 3 hours of googleing couldn't figure out how to do it.
And I'd like the cursor to go back to txtCode after clicking ok on all those error messages. The cursor keeps going to the next textbox in the tab order :(

thanks,

sessionone

Tommy
05-11-2009, 07:07 PM
This is what I think you need/looking for. I commented the code some anyway LOL , I didn't update the fields. If there is anything else I can help with let me know.


Private Sub txtCode_Change()
'check to see if it is numeric - this will complain if
'the last character entered is alpha
If Not IsNumeric(txtCode.Text) Then
Call MsgBox("The Entry is Invalid - not Numeric!", vbCritical, "Entry MUST be Numeric!")
'since the last character entered is alpha remove it
txtCode.Text = Left(txtCode.Text, Len(txtCode.Text) - 1)
End If
End Sub

Private Sub txtCode_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'when leaving the textbox chack for the length of the text
If Len(txtCode.Text) <> 11 Then
'tell them what they did wrong and don't let them leave
Call MsgBox("Please Enter 11 Digits Only!", vbInformation, "Must Enter 11 Digits!")
Cancel = True
Else
'make sure the first character is a 3 or 4
If Left(txtCode.Text, 1) <> "3" Then
If Len(txtCode.Text) <> "4" Then
'tell them what they did wrong and don't let them leave
Call MsgBox("The First Digit Incorrect!", vbInformation, "The first Digit must be a 3 or 4!")
Cancel = True
Else ' first letter is 4
'Do something
End If
Else ' first letter is 3
'Do something
End If
End If
End Sub


:D

sessionone
05-18-2009, 02:19 PM
Thanks a lot for this, Tommy. Didn't have enough time to sit down and check it.

It works fine;however, there's a slight problem. After entering the wrong code (all 1s for example) and deleting the whole thing by pressing backspace, it shows the error message and goes to debugging mode at this line:
txtCode.Text = Left(txtCode.Text, Len(txtCode.Text) - 1)
Run-time error '5'. Invalid procedure call or argument.
Any suggestions?

Cheers,
sessionone

fumei
05-19-2009, 12:59 PM
1. It goes into debug mode because if "After entering the wrong code (all 1s for example) and deleting the whole thing by pressing backspace"....

then Len(txtCode.Text) - 1 is an error.

2. I would use Select Case rather than multiple If...Then, as you are testing multiple values for ONE variable - which is what Select Case is designed for.
Select Case Left(txtCode.Text, 1)
Case "3" ' if it starts with 3
' do what ever
Case "4" 'if it starts with 4
' do what ever
Case Else ' if is NOT either 3 or 4
' do whatever, presumably a message to
' the user and returning the cursor to txtCode
End Select

Tommy
05-20-2009, 11:12 AM
Hi sessionone,

The below code will fix my error.

if Len(txtCode.Text) > 0 then
txtCode.Text = Left(txtCode.Text, Len(txtCode.Text) - 1)
endif

Also the select case example Gerry(fumei) has posted is a good option also.

fumei
05-20-2009, 11:20 AM
And that is the right error trapping for an "empty" textbox.

sessionone
05-20-2009, 03:12 PM
Hi,

Thanks guys. It works, so this is sorted. Now to the next big question! :)

yours truly
sessionone