Results 1 to 3 of 3

Thread: Searching for specific set of characters in a label caption?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Searching for specific set of characters in a label caption?

    Hi everyone! I am working on a review game for students, and the beauty of this one is the students come up with the review questions, so they really want to add a lot of questions. Anyway I have two label captions.

    Label1.caption and Label2.caption.

    The question goes in Label1.caption and might be something like (1). What county is the Nile River located in?



    What I am wondering is, how do I search for the number that is in the brackets? In this case 1 and just copy that number, and put it in label2.caption? The questions themselves will never have any brackets, so they won't confuse any search code, but has anyone tried this before? I would really appreciate it!

    Thanks.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Searching for specific set of characters in a label caption?

    What you need to use is Mid$, Instr and (optionally) InstrRev functions:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Label1.Caption = "(1). What county is the Nile River located in?"
    End Sub
    
    Private Sub Command1_Click()
        Label2.Caption = VBA.Mid$(Label1.Caption, _
                                InStr(1, Label1.Caption, "(") + 1, _
                                InStrRev(Label1.Caption, ")") - 1 - InStr(1, Label1.Caption, "("))
    End Sub

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Searching for specific set of characters in a label caption?

    Try this
    Code:
    Private Sub Command1_Click()
        MsgBox GetQuestionNumber("(1). What county is the Nile River located in?")
    End Sub
    
    Private Function GetQuestionNumber(ByVal strText As String) As String
        Dim p1 As Long
        Dim p2 As Long
        Dim r As String
        
        p1 = InStr(1, strText, "(")
        p2 = InStr(1, strText, ")")
        If p1 And p2 Then
            r = Mid$(strText, p1 + 1, p2 - p1 - 1)
        End If
        
        GetQuestionNumber = r
    End Function
    BTW: the answer is my country Egypt



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width