WUGNET, the Windows User Group Network
Your Complete Resource Center for "The Best" in Shareware, Computing Tips and Support, Windows Industry News... and much more!
Home Forums Shareware Windows Tips Hot Offers FREE Newsletters Arcade Contact Us About Partners
Search WUGNET: RSS Feeds RSS Feeds Advertise with WUGNET    |    Shareware eBooks
HomeHome FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

validating form data in word with If statements

 
   Home -> Office -> User Forms RSS
Next:  Unable to edit my calendar when viewing multiple ..  
Author Message
Ed Greenbank

External


Since: May 20, 2009
Posts: 2



(Msg. 1) Posted: Wed May 20, 2009 4:05 pm
Post subject: validating form data in word with If statements
Archived from groups: microsoft>public>word>vba>userforms (more info?)

I hope this is not in the wrong forum. I am quite new to VBA but I am trying
to create a form in word with form fields that the user will fill in. At the
end I have made a "validate" control button. This runs various "IF"
statements to check each field has been filled in correctly before it
submits the data to an email address.

My problem is I do not know how to refer to my form fields in the IF's
properly.

I have been doing this:

Sub example()
With ActiveDocument.FormFields("TypeYourName")
If Len(.Result) < 4 or (.Result) = "TypeYourName" Then
MsgBox "Please insert your full name", vbInformation
ActiveDocument.Bookmarks("TypeYourName").Range.Fields(1).Result.Select
End
Else
End If
End With

etc.


The problem is, I want to refer to multiple form fields/bookmarks in the
same IF to be able to do something like..

If (IsThisAnAddressableLocation) ="Yes" and Len(HouseNumber) < 1 Then
Msgbox "You have stated that this an addressable location so please provide
a house number"
End
Else
End If

If (IsThisAnAddressableLocation) = "No" and Len(HouseNumber) > 0 Then
Msgbox "You have stated that this is not an addressable location yet have
provided a house number"
End
Else
End If

I know there is probably better ways I could have gone about this form but
its all set up and works great, and if I had an answer to my problem I could
complete the validation stage a lot sooner.

Many thanks and I hope this makes sense
Back to top
Login to vote
Doug Robbins - Word MVP

External


Since: Jul 14, 2006
Posts: 4293



(Msg. 2) Posted: Wed May 20, 2009 6:05 pm
Post subject: Re: validating form data in word with If statements [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

For your example, it would be best to nest the If...then...Else
constructions as follows

If ActiveDocument.FormFields("IsThisAnAddressableLocation").Result ="Yes"
Then
If Len(ActiveDocument.FormFields("HouseNumber").Result) < 1 Then
Msgbox "You have stated that this an addressable location so please
provide a house number"
End If
Else 'The address is not an addressable location, so there should be no
house number
If Len(ActiveDocument.FormFields("HouseNumber").Result) > 0 Then
Msgbox "You have stated that this is not an addressable location yet
have provided a house number"
End If
End If



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"Ed Greenbank" <egreenbank.RemoveThis@gmail.com> wrote in message
news:%23VSZ95X2JHA.4412@TK2MSFTNGP06.phx.gbl...
>I hope this is not in the wrong forum. I am quite new to VBA but I am
>trying to create a form in word with form fields that the user will fill
>in. At the end I have made a "validate" control button. This runs various
>"IF" statements to check each field has been filled in correctly before it
>submits the data to an email address.
>
> My problem is I do not know how to refer to my form fields in the IF's
> properly.
>
> I have been doing this:
>
> Sub example()
> With ActiveDocument.FormFields("TypeYourName")
> If Len(.Result) < 4 or (.Result) = "TypeYourName" Then
> MsgBox "Please insert your full name", vbInformation
> ActiveDocument.Bookmarks("TypeYourName").Range.Fields(1).Result.Select
> End
> Else
> End If
> End With
>
> etc.
>
>
> The problem is, I want to refer to multiple form fields/bookmarks in the
> same IF to be able to do something like..
>
> If (IsThisAnAddressableLocation) ="Yes" and Len(HouseNumber) < 1 Then
> Msgbox "You have stated that this an addressable location so please
> provide a house number"
> End
> Else
> End If
>
> If (IsThisAnAddressableLocation) = "No" and Len(HouseNumber) > 0 Then
> Msgbox "You have stated that this is not an addressable location yet have
> provided a house number"
> End
> Else
> End If
>
> I know there is probably better ways I could have gone about this form but
> its all set up and works great, and if I had an answer to my problem I
> could complete the validation stage a lot sooner.
>
> Many thanks and I hope this makes sense
>
Back to top
Login to vote
Ed Greenbank

External


Since: May 20, 2009
Posts: 2



(Msg. 3) Posted: Thu May 21, 2009 4:05 pm
Post subject: Re: validating form data in word with If statements [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks Doug, I will try that tomorrow in work.
I dont actually have MS Word at home, only OpenOffice "Writer".

I tried to set up a similar scenario to test your code but do not seem to be
able to get any code to work whatsoever... perhaps "ActiveDocument" and
"FormFields" should be something else when using Open Office Writer.

Really apprieciate the help though and the work I have done so far would not
have been possible without the help of word.mvps.org


"Doug Robbins - Word MVP" <dkr.RemoveThis@REMOVECAPSmvps.org> wrote in message
news:e$e58EZ2JHA.1424@TK2MSFTNGP02.phx.gbl...
> For your example, it would be best to nest the If...then...Else
> constructions as follows
>
> If ActiveDocument.FormFields("IsThisAnAddressableLocation").Result ="Yes"
> Then
> If Len(ActiveDocument.FormFields("HouseNumber").Result) < 1 Then
> Msgbox "You have stated that this an addressable location so please
> provide a house number"
> End If
> Else 'The address is not an addressable location, so there should be no
> house number
> If Len(ActiveDocument.FormFields("HouseNumber").Result) > 0 Then
> Msgbox "You have stated that this is not an addressable location
> yet have provided a house number"
> End If
> End If
>
>
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
> "Ed Greenbank" <egreenbank.RemoveThis@gmail.com> wrote in message
> news:%23VSZ95X2JHA.4412@TK2MSFTNGP06.phx.gbl...
>>I hope this is not in the wrong forum. I am quite new to VBA but I am
>>trying to create a form in word with form fields that the user will fill
>>in. At the end I have made a "validate" control button. This runs various
>>"IF" statements to check each field has been filled in correctly before it
>>submits the data to an email address.
>>
>> My problem is I do not know how to refer to my form fields in the IF's
>> properly.
>>
>> I have been doing this:
>>
>> Sub example()
>> With ActiveDocument.FormFields("TypeYourName")
>> If Len(.Result) < 4 or (.Result) = "TypeYourName" Then
>> MsgBox "Please insert your full name", vbInformation
>> ActiveDocument.Bookmarks("TypeYourName").Range.Fields(1).Result.Select
>> End
>> Else
>> End If
>> End With
>>
>> etc.
>>
>>
>> The problem is, I want to refer to multiple form fields/bookmarks in the
>> same IF to be able to do something like..
>>
>> If (IsThisAnAddressableLocation) ="Yes" and Len(HouseNumber) < 1 Then
>> Msgbox "You have stated that this an addressable location so please
>> provide a house number"
>> End
>> Else
>> End If
>>
>> If (IsThisAnAddressableLocation) = "No" and Len(HouseNumber) > 0 Then
>> Msgbox "You have stated that this is not an addressable location yet have
>> provided a house number"
>> End
>> Else
>> End If
>>
>> I know there is probably better ways I could have gone about this form
>> but its all set up and works great, and if I had an answer to my problem
>> I could complete the validation stage a lot sooner.
>>
>> Many thanks and I hope this makes sense
>>
>
Back to top
Login to vote
Display posts from previous:   
       Home -> Office -> User Forms All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Categories:
 Windows XP
 Windows Vista
 Windows Other
  Office
 Office Other
 Security
  • Home |
  • Shareware |
  • Windows Tips |
  • Hot Offers |
  • FREE Newsletters |
  • Arcade |
  • Forums |
  • eBooks |
  • About WUGNET |
  • Partners |
  • Contact

  • WUGNET Privacy Policy |
  • Link to WUGNET |
  • IT Support