Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    VBA input box cancel removes value

    I have

    Code:
    If IsValidEmail([TeacherEmail]) Or IsNull([TeacherEmail]) Then
    'do nothing
    Else
    [TeacherEmail] = InputBox(IIf(IsNull([MergedName]), "The Teacher's", "The teacher " & [MergedName]) & " email " & [TeacherEmail] & " is invalid, please update below", "Invalid Email Found", [TeacherEmail])
    End If
    however if the user clicks cancel - the email becomes null/""

    What would be the correct way to do this as I can't be removing emails if the user cancels

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Set a variable to the InputBox. Then test the variable.

    If strInput <> "" Then Me.TeacherEmail = strInput
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Set a variable to the InputBox. Then test the variable.

    If strInput <> "" Then Me.TeacherEmail = strInput
    I have

    If IsNull(emailUpdate) Or "" ThenElse
    email1 = emailUpdate
    End If

    it's underneath

    would it do the same thing as what you have?

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I've made a module with

    Code:
    Function fixTeacherEmail(email1, Tname As Control)
    Dim emailUpdate As String
    If IsValidEmail(email1) Or IsNull(email1) Then
    'do nothing
    Else
    emailUpdate = InputBox(IIf(IsNull(Tname), "The Teacher's", "The teacher " & Tname) & " email " & email1 & " is invalid, please update below", "Invalid Email Found", email1)
    If IsNull(emailUpdate) Or "" Then
    Else
    email1 = emailUpdate
    End If
    End If
    End Function
    then I call

    call fixTeacherEmail([TeacherEmail],[MergedName])

    look right to you?

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    updated it to

    Code:
    Function fixTeacherEmail(TeacherEmail1, Tname As Control)
    Dim strEmail1, strName1, emailUpdate As String
    strEmail1 = IIf(IsNothing(TeacherEmail1), "", Tname)
    strName1 = IIf(IsNothing(Tname), "", Tname)
    
    
    
    
    If IsValidEmail(strEmail1) Or IsNull(strEmail1) Then
    'do nothing
    Else
    emailUpdate = InputBox(IIf(IsNull(strName1), "The Teacher's", "The teacher " & strName1) & " email " & strEmail1 & " is invalid, please update below", "Invalid Email Found", strEmail1)
    If IsNull(emailUpdate) Or "" Then
    Else
    TeacherEmail1 = emailUpdate
    End If
    End If
    End Function
    update: argh it's hard to keep track!

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    My problem is now that isnothing() needs a string

    how do I get [TeacherName] as a string when I make the function have "as control" ?
    is as control the correct one to use??

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Why a fixTeacherEmail function?

    Why not just code in the original event?

    Why an InputBox anyway? Why not bound textbox?

    A string variable cannot hold null. Code will error if try to set to null. Only a variant can hold null.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Why a fixTeacherEmail function?

    Why not just code in the original event?

    Why an InputBox anyway? Why not bound textbox?

    A string variable cannot hold null. Code will error if try to set to null. Only a variant can hold null.
    I have to use this a lot for various forms.

    A popup is easier.

    Thanks - will see if variant will do.

  9. #9
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I get

    Click image for larger version. 

Name:	Capture.PNG 
Views:	13 
Size:	5.8 KB 
ID:	15745

    on
    IsValidEmail(strEmail1)

    for ref
    Code:
    Function fixTeacherEmail(TeacherEmail1, Tname As Variant)
    
    
    Dim strEmail1, strName1, emailUpdate As String
    strEmail1 = IIf(IsNothing(TeacherEmail1) Or IsNull(TeacherEmail1), "", TeacherEmail1)
    strName1 = IIf(IsNothing(Tname) Or IsNull(Tname), "", Tname)
    
    
    If IsValidEmail(strEmail1) Or IsNull(strEmail1) Then
    'do nothing
    Else
    emailUpdate = InputBox(IIf(IsNull(strName1), "The Teacher's", "The teacher " & strName1) & " email " & strEmail1 & " is invalid, please update below", "Invalid Email Found", strEmail1)
    If IsNull(emailUpdate) Or "" Then
    Else
    TeacherEmail1 = emailUpdate
    End If
    End If
    
    
    End Function
    update *me thinks* IsValidEmail(strEmail1.Value)

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,648
    Which line triggers that error?

    Are you aware that VBA requires every variable to be explicitly declared or they will default as variant?

    Your Dim line actually sets only emailUpdate as a string variable. All others on that line are variant.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Which line triggers that error?

    Are you aware that VBA requires every variable to be explicitly declared or they will default as variant?

    Your Dim line actually sets only emailUpdate as a string variable. All others on that line are variant.
    oh crap, I thought it did that for all of them

  12. #12
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I'll start from the beginning

    I have on the record a onclick event for a button

    Code:
    Dim strEmailSchool As String
    Dim strEmailTeacher As String
    Dim strSchoolName As String
    Dim strTeacherName As String
    
    
    strSchoolName = [SchoolName]
    strEmailSchool = [SchoolEmail]
    
    
    strTeacherName = [MergedName]
    strEmailTeacher = [TeacherEmail]
    
    
    
    
    
    
    'repair Emails first
    Call fixSchoolEmail(strEmailSchool, strSchoolName)
    Call fixTeacherEmail(strEmailTeacher, strTeacherName)
    Then I made a function that would take the strings and run them through a function that I want to use a lot

    Code:
    Function fixTeacherEmail(TeacherEmail1, Tname As String)
    
    
    Dim strEmail1 As String
    Dim strName1 As String
    Dim emailUpdate As String
    
    
    strEmail1 = TeacherEmail1
    strName1 = Tname
    
    
    If IsValidEmail(strEmail1) Or IsNull(strEmail1) Then
    'do nothing
    Else
    emailUpdate = InputBox(IIf(IsNull(strName1), "The Teacher's", "The teacher " & strName1) & " email " & strEmail1 & " is invalid, please update below", "Invalid Email Found", strEmail1)
    If IsNull(emailUpdate) Or "" Then
    Else
    TeacherEmail1 = emailUpdate
    End If
    End If
    
    
    End Function
    Basically it doesn't come up with the error now but it doesn't popup either or give me any feedback

    update it works now only I have to set the control to = the string (I would rather import the [fieldvalue] directly and have it change within the function)

  13. #13
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Simplified

    Code:
    Call fixSchoolEmail([SchoolEmail], [SchoolName])
    Call fixTeacherEmail([TeacherEmail], [MergedName])
    Code:
    Function fixTeacherEmail(strTEmail As String, strTName As String)
    
    
    Dim emailUpdate As String
    
    
    If IsValidEmail(strTEmail) Or IsNull(strTEmail) Then
    'do nothing
    Else
    emailUpdate = InputBox(IIf(IsNull(strTName), "The Teacher's", "The teacher " & strTName) & " email " & strTEmail & " is invalid, please update below", "Invalid Email Found", strTEmail)
    If IsNull(emailUpdate) Or "" Then
    Else
    strTEmail = emailUpdate
    End If
    End If
    
    
    End Function

  14. #14
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Oh I see what I am doing wrong here

    fixTeacherEmail should be assigned...

  15. #15
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    ok I think I have it sorted

    I have made

    Code:
    Dim strScEmailchange As String
    
    
    strScEmailchange = fixEmail([SchoolEmail], [SchoolName])
    [SchoolEmail] = strScEmailchange
    
    
    strScEmailchange = fixEmail([TeacherEmail], [MergedName])
    [TeacherEmail] = strScEmailchange
    and

    Code:
    'fix an email
    Function fixEmail(strSemailPull As Variant, strSnamePull As Variant) As String
    
    
    Dim strEmail As String
    Dim strEName As String
    Dim strInput As String
    
    
    strEmail = IIf(IsNull(strSemailPull), "", strSemailPull)
    strEName = IIf(IsNull(strSnamePull), "", strSnamePull)
    
    
           
            If IsValidEmail(strEmail) = True Then
            fixEmail = strEmail
            
            Else
            
            strInput = InputBox(strEName & " has an invalid email, please correct below.", "Bad Email", strEmail)
            
                If strInput <> "" Then
                fixEmail = strInput
                End If
                
            End If
    
    
    End Function
    and it all seems to work - can you spot anything wrong?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 12-12-2013, 10:13 PM
  2. Replies: 2
    Last Post: 04-11-2013, 06:36 PM
  3. update query that removes extra letters
    By alinapotter in forum Queries
    Replies: 4
    Last Post: 04-06-2013, 01:15 PM
  4. Replies: 3
    Last Post: 11-22-2012, 08:01 PM
  5. Adding table to Query removes every 4th record
    By CoachBarker in forum Queries
    Replies: 1
    Last Post: 10-13-2010, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums