Link to home
Start Free TrialLog in
Avatar of etdreaming
etdreaming

asked on

validating user input when using vba

Below is the code I'm using in powerpoint but I'm trying to self teach myself vba and I'm a little stuck here.  This code runs just fine but what I want to do is validate the 6 digit id number and the user name.  I don't want the message box to export the data to my csv file if he enters more or less than 6 digits or if he enters text.  I want it to simply pop up over and over again until he enters exactly 6 digits.  Same thing with the message box asking for his first name.  If he enters numbers instead of text I don't want it to record the data unless he enters text.  I want that one to also pop up over and over until he enters text.  I know it can be done but I'm clueless as to how.  thx!!
Mike

Public Sub Driverid()
' after driver inputs his id# it will export it to a csv file
Dim strID As String ' this can be local if you don't use it anwher else!
strID = InputBox(prompt:="Please enter your 6 digit driver ID#", _
          Title:="Welcome to Excellence through Education!")
Open "C:\Documents and Settings\mikee\Desktop\testfile.csv" For Append As #1
Print #1, strID
Close #1
mUserName = InputBox(prompt:="Type your FIRST name only", Title:="Personalize")
ActivePresentation.SlideShowWindow.View.Next
End Sub
Avatar of dqmq
dqmq
Flag of United States of America image

Here's one way--for the first part at least.  I'm betting you can't figure out the second part:


Public Sub Driverid()
' after driver inputs his id# it will export it to a csv file
Dim strID As String ' this can be local if you don't use it anwher else!
dim strPrompt as string
strID = ""
strPrompt = ""
Do while strID = ""
  strPrompt = strPrompt & "Please enter your 6 digit driver ID#"
  strID = InputBox(prompt:=strPrompt, _
          Title:="Welcome to Excellence through Education!")
  strPrompt = ""
  If len(strID) <> 6 then
       strID = ""   'ask again
       strPrompt = strPrompt & "At least 6 digits are required" & vbNewLine
  end if
  If  isnumeric(strID) = False
        strID = ""   'ask again
       strPrompt = strPrompt & "Only numbers are allowed" & vbNewLine
  end if
Loop

Open "C:\Documents and Settings\mikee\Desktop\testfile.csv" For Append As #1
Print #1, strID
Close #1
mUserName = InputBox(prompt:="Type your FIRST name only", Title:="Personalize")
ActivePresentation.SlideShowWindow.View.Next
End Sub
Avatar of etdreaming
etdreaming

ASKER

It doesn't like < If  isnumeric(strID) = False > I get a syntax error.  Any ideas?
yeah... add a "then"  :>)
I appreciate you hanging in there with me but as I mentioned I'm learning vba as I go....I've tried adding "then" in about every spot I can and to no avail.  You'll have to type out exactly how the line should look because I'm not having any luck.  Also, any ideas on the last part of the original question about forcing the user to use text when entering their name?  thx.
Mike
Scratch that.........I just figured out where to put the, "then".  First part is working great.  Just need some help on the final part dealing with the name.  thx!
ASKER CERTIFIED SOLUTION
Avatar of dqmq
dqmq
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent.  thx much.
Mike