Results 1 to 3 of 3
  1. #1
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73

    Type mismatch from InputBox

    Hello. This my second post this afternoon. Sorry to be a bother, but I am really trying to learn.

    I have two text boxes. I am trying to learn some VBA, and am working with strings in the book I am in. This code, asking for user input to decide how many characters to retrieve from the right works just fine:

    Private Sub cmdRight2_Click()
    Dim sAmount As String
    Dim iAmount As Integer
    Dim iStrLen As Integer

    iStrLen = Len(txtInput.Value)

    sAmount = InputBox("How many characters would you like to " & _
    " retrieve?", "How Many")



    If sAmount <> "" Then

    iAmount = sAmount
    If iAmount > iStrLen Then
    MsgBox "There are not that many characters"

    Else
    txtOutput.Value = Right(txtInput.Value, iAmount)


    End If
    End If
    End Sub









    This code, to get user input to decide how many characters to use with the Mid command does not work:

    Private Sub cmdMid2_Click()
    Dim sAmount1 As Integer
    Dim sAmount2 As Integer
    Dim iAmount1 As Integer
    Dim iAmount2 As Integer
    Dim iStrLen1 As Integer
    Dim iStrLen2 As Integer




    iStrLen1 = Len(Me.txtInput.Value)

    sAmount1 = InputBox("Which character do you want to" & _
    " start with?", "Which Character?")
    If sAmount1 <> "" Then

    iAmount1 = sAmount1

    If iAmount1 > iStrLen1 Then
    MsgBox "There are not that many characters"

    Else
    iStrLen2 = iStrLen1 - iAmount1

    sAmount2 = InputBox("How many characters would you like to" & _
    " retrieve?", "How Many?")

    If sAmount2 <> "" Then

    iAmount2 = sAmount2

    If iAmount2 > iStrLen2 Then
    MsgBox "There are not that many characters left"
    Else

    Me.txtOutput.Value = Mid(Me.txtInput.Value, iAmount1, iAmount2)
    End If
    End If
    End If
    End If


    End Sub

    Well, it actually works fine if I comment out

    If sAmount1 <> "" Then
    and
    If sAmount2 <> "" Then

    and two of the End If statements. Unless, of course, I hit cancel. If I do that, or if I do not comment them out, I get Type Mismatch errors.

    Can someone tell me what the heck I am doing wrong?

    Thanks!

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    sAmount can NEVER be "" because it is a NUMBER. You can use

    If sAmount1 <> 0 Then

  3. #3
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73
    Oh for crying out loud. THAT is the difference between the two codes. I feel like a fool. Thanks for the help.. I should have seen that.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Data Type Mismatch
    By timmy in forum Programming
    Replies: 9
    Last Post: 04-12-2011, 03:48 AM
  2. Type Mismatch....WHY?!!?
    By jgelpi16 in forum Queries
    Replies: 9
    Last Post: 03-07-2011, 09:18 AM
  3. Type Mismatch
    By Alex Motilal in forum Programming
    Replies: 10
    Last Post: 02-13-2011, 05:42 AM
  4. Type mismatch
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 08-07-2010, 06:54 AM
  5. Type Mismatch - HELP!
    By jgelpi in forum Programming
    Replies: 3
    Last Post: 07-17-2009, 03:53 PM

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