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

    InStr and Mid code creating type mismatch

    Back again!

    I have another issue where I do not understand why one code works and another does not.

    First, I have this:

    Private Sub cmdInStr_Click()
    Dim sExtract As String
    Dim sStartPosition As String

    'search the input string for a space character
    sStartPosition = InStr(txtInput.Value, " ")

    'Extract after a space

    sExtract = Mid(txtInput.Value, sStartPosition + 1)



    Me.txtOutput.Value = "Thanks Mr. " & sExtract


    End Sub

    Where you put a name in the Input box and it pulls the last name. It works fine.





    This, on the other hand:

    Private Sub cmdInStr2_Click()
    Dim sExtract As String
    Dim sStartPosition As String


    sStartPosition = InputBox("What word or character would you like" & _
    " to begin with?", "Where to begin?")
    If sStartPosition <> "" Then

    If InStr(txtInput.Value, sStartPosition) = 0 Then
    MsgBox "That value does not exist"
    Else

    sExtract = Mid(txtInput.Value, sStartPosition)

    Me.txtOutput.Value = sExtract
    End If
    End If


    End Sub

    Is supposed to look at the text in Input and asks the user what character(s) they want to use to start looking for, and begin extracting there. This code, however, kicks back a type mismatch at the

    sExtract = Mid(txtInput.Value, sStartPosition)

    portion of the code, and the first code does not. Last time I asked a question like this I stupidly declared variables that were supposed to be strings as integers, but I didn't do that this time.

    I really appreciate the help!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    if txtInput is a text box on a form reference it using me.txtinput not txtInput.value.

  3. #3
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73
    Quote Originally Posted by rpeare View Post
    if txtInput is a text box on a form reference it using me.txtinput not txtInput.value.

    I gave that a try, but I got the same error.

    If it helps, I can see that txtInput.Value contains what I expect it to contain, as does sStartPosition. (by hovering over them in the debugger)

    Thanks!

  4. #4
    michaelplogue is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    4
    Mid requires a start and end value. You've only got the start....

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The main problem is that the second argument of the Mid() function requires a LONG, not a string. Even though in the first example "sStartPosition" is a string, Access casts (changes) it from a "string" number to a Long, so it returns a value.

    In the second example, you are trying to use a string value (for example 'the') in "sStartPosition" as the second argument of the MID() function.

    While the function MID() has three arguments, the last argument, Length, is optional.

    Try this:

    Code:
    Private Sub cmdInStr2_Click()
       Dim sExtract As String
       Dim sInput As String
       Dim sStartPosition As Long
    
    
       sInput = InputBox("What word or character would you like" & _
                         " to begin with?", "Where to begin?")
       If sInput <> "" Then
    
          If InStr(txtInput, sInput) = 0 Then
             MsgBox "That value does not exist"
          Else
          
             sStartPosition = InStr(txtInput, sInput)
             sExtract = Mid(txtInput, sStartPosition)
    
             Me.txtOutput = sExtract
          
          End If
       End If
    
    End Sub
    Note that I removed ".Value" because value is the default property. It doesn't hurt to add it, but it is not required ( and no one uses it to save time and typing).

  6. #6
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73
    That works perfectly! More importantly, you made it make sense. I really appreciate it. I am sure I will be back soon...

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    hah good catch you guys!

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

Similar Threads

  1. Type Mismatch....WHY?!!?
    By jgelpi16 in forum Queries
    Replies: 9
    Last Post: 03-07-2011, 09:18 AM
  2. Type Mismatch
    By Alex Motilal in forum Programming
    Replies: 10
    Last Post: 02-13-2011, 05:42 AM
  3. Type mismatch
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 08-07-2010, 06:54 AM
  4. Type Mismatch - HELP!
    By jgelpi in forum Programming
    Replies: 3
    Last Post: 07-17-2009, 03:53 PM
  5. Replies: 4
    Last Post: 05-16-2009, 09:17 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