If Staement question

C

coconutt

I have a macro
Sub Save2()
Dim vRefferingPhysician, vDate As String
If vRefferingPhysician = "Swain"
Then
Save
Else
SaveR
End If
End Sub
when I run the code it will always run the saveR macro, even if the field
is swain am I asking the right if question?
Thanks
 
E

Ed

How are you getting the field value of "Swain" into your variable
vRefferingPhysician?
(Note1: If you want vRefferingPhysician to be a String variable, then you
must assign it -
Dim vRefferingPhysician As String, vDate As String
Otherwise it is by default a "variable", which could possibly be interpreted
as an incorrect value.)
(Note2: If you are trying to pass vRefferingPhysician in from another
procedure or function, or pick it up from a field code, is it possible you
have it spelled correctly in the other place (vReferringPhysician), and the
typo is causing you difficulties?)
Ed
 
J

Jay Freedman

coconutt said:
I have a macro
Sub Save2()
Dim vRefferingPhysician, vDate As String
If vRefferingPhysician = "Swain"
Then
Save
Else
SaveR
End If
End Sub
when I run the code it will always run the saveR macro, even if the
field is swain am I asking the right if question?
Thanks

Hi coconutt,

The question is OK (almost) but you've neglected to give the variable
vRefferingPhysician any value, so the comparison can never be true.

What is "the field"? If it's a text form field in a protected form, use the
name assigned to it in the field's Properties dialog to do something like
this before the If statement:

vRefferingPhysician = _
ActiveDocument.FormFields("RefferingPhysician").Result

The "almost" part is that the comparison is case-sensitive, so you probably
should do this instead:

If LCase(vRefferingPhysician) = "swain"

One more bit, not critical but a bad habit: By omitting the "As" clause for
vRefferingPhysician in your Dim statement, you've implicitly declared it as
Variant data type, not String. Each variable in a Dim statement needs its
own As clause. It should be

Dim vRefferingPhysician As String, vDate As String

(And what is vDate? you don't use it.)

Lastly, my sensibility is offended by the misspelling of 'referring'. ;-)
 
C

coconutt

here are some snippets

Dim vRefferingPhysician, vPatientName, vCurrentTreatment As String
'setting variables to the form
vRefferingPhysician = frmInfoForm.txtRefferingPhysician.Text
'Populating the word document bookmarks
ActiveDocument.FormFields("bkRefPhysician").Result = vRefferingPhysician

all this works fine when i click ok on my info form
I doublechecked the spelling, and stepped through the code and it holds the
info.
should i do something else?
 
E

Ed

First, put the As String declaration after each of your string variables.
Putting them altogether on one line with As String at the end does NOT make
them all string variables (I did this, too, in my beginning days (not too
long ago!), and ran into odd problems.)
Dim vRefferingPhysician As String, vDate As String

Next, load a text string into the variable
vRefferingPhysician =
ActiveDocument.Bookmarks("bkRefPhysician").Range.Text
(As a test, I would do something like
MsgBox vRefferingPhysician
to make sure you have the expected text in the string.)

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top