Results 1 to 2 of 2
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,369

    Blank String

    Hi Guy's what am I doing wrong here, I am trying to get either Forename only (Joe) or if the title <> "" then I am trying to get title and surname (Mr Bloggs)

    The MsgBox is blank



    Code:
    Dim MyStr As String, fName As String, SName As String, mHello as String
    If Me.txtClientName <> "" Then
    If Me.cboClientTitle = "" Then
    MyStr = InStr(Me.txtClientName, " ")
    fName = Left(Me.txtClientName, MyStr) 'Forename
    SName = Right(MyStr, MyStr) 'Surname
    mHello = fName
    Else
    fName = ""
    SName = ""
    End If
    End If
    If Me.txtClientName <> "" Then
    If Me.cboClientTitle <> "" Then
    MyStr = InStr(Me.txtClientName, " ")
    fName = Left(Me.txtClientName, MyStr) 'Forename
    SName = Right(MyStr, MyStr) 'Surname
    mHello = Me.cboClientTitle & " " & SName
    Else
    fName = ""
    SName = ""
    End If
    End If
    MsgBox (mHello)

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Code would be easier to read with indentation.

    Repeating If conditions is not logical.

    More likely an empty field is Null, not an empty string, so handle both possibilities.

    InStr() returns a number, not a string.

    MsgBox with parens is a function returning a value. For a popup message that user just closes, don't need parens, although looks like will work.

    Consider:

    Code:
    Dim strName As String
    strName = Nz(Me.txtClientName, "")
    If strName <> "" Then
        If Me.cboClientTitle & "" = "" Then
            MsgBox Left(strName, InStr(strName, " ")) 'Forename
        Else
            MsgBox Me.cboClientTitle & Mid(strName, InStr(strName, " ")) 'Title and Surname
        End If
    End If
    Name parts really should be separate fields in table.
    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.

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

Similar Threads

  1. Replies: 5
    Last Post: 06-15-2018, 03:14 PM
  2. Why do I have an extra blank string?
    By Ramtrap in forum Programming
    Replies: 5
    Last Post: 03-22-2018, 08:37 AM
  3. Replies: 1
    Last Post: 01-31-2018, 04:06 PM
  4. Replies: 6
    Last Post: 11-17-2015, 09:16 AM
  5. Replies: 2
    Last Post: 04-05-2015, 06:06 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