Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Launching a message box makes code work, fails otherwise / Hyperlink 1

Status
Not open for further replies.

AHJ1

Programmer
Oct 30, 2007
69
US
Displaying a message box causes a difference in the execution of the following code. See '@ When I run the code with the message box, it properly displays the number of occurrences of the string "ID=", and only writes a line to a table when the condition of the test is met. When I take out the MsgBox, every URL, even those that have "0" occurrences of the "ID=0" are included in the table, and the variable intNoOfRef remains the same instead of having fresh data inserted into it.

Also, a quick question: When I insert this URL into a field in an access database of the type hyperlink, the text is inserted, but a hyperlink is not created. Any advice?

Code:
Function Test(strPagesFirstSeenByGoogle As String)
 On Error GoTo err_QueryGoogle1
'Variables from table to build keyword string
    Dim strKeywordIn1 As String
    Dim strKeywordIn2 As String
    Dim strKeywordIn3 As String
    Dim strRelevantToIn As String
    
    Dim strReasonIn As String
    Dim cn As ADODB.Connection
    Dim rst As ADODB.Recordset
    
    Dim strAmendments As String
    Dim strBills As String
    
    Dim strSQL As String
    Dim intNoOfRef As Integer
    Dim i As Integer
    If IsNull(strPagesFirstSeenByGoogle) Then strPagesFirstSeenByGoogle = "d" 'Last 24 hours
    'Other values, if we want m for month, y for year, m2, etc. 2 months, y2, etc = two years
'Open connection and recordset, using a predefined query
    Set cn = CurrentProject.Connection
    Set rst = cn.Execute("qryKeywords4Depts")
    DoCmd.OpenForm "frmWebscrape", acNormal
    Do While Not rst.EOF
    'Initialize
    strKeywordIn1 = ""
    strKeywordIn2 = ""
    strKeywordIn3 = ""
    gstrInnerText = ""
    DoCmd.SetWarnings False
    'Loop through the query results to generate words to place into the SQL statement
        If Not IsNull(rst("Keyword")) Then strKeywordIn1 = rst("Keyword")
        If Not IsNull(rst("AddKeyword1")) Then strKeywordIn2 = rst("AddKeyword1")
        If Not IsNull(rst("AddKeyword2")) Then strKeywordIn3 = rst("AddKeyword2")
        
   
    'Create a variable string for bills that have passed
        strBills = AllowApostrophes("[URL unfurl="true"]http://www.google.com/search?q="[/URL] & Chr$(34) & strKeywordIn1 & Chr$(34) & " + " & Chr$(34) & strKeywordIn2 & Chr$(34) & " site:[URL unfurl="true"]http://www.leg.state.nv.us/74th/Reports&num=100&hl=en&lr=&as_qdr="[/URL] & strPagesFirstSeenByGoogle & "filter=0")
 
        i = i + 1
     '@
        GoToURL (strBills)
        GetHTML
         intNoOfRef = STCountIf(gstrInnerText, "ID=", False)
         MsgBox "intNoOfRef for phrase ID = " & intNoOfRef & " in " & vbCrLf & gstrInnerText
        If intNoOfRef >= 1 Then
           'There is a match, and we need to put this URL and number of hits into a table for review
            DoCmd.SetWarnings False
            Debug.Print strBills & " - " & intNoOfRef
            strSQL = "INSERT INTO WebHits (WebPageHit,NoOfRef) VALUES ('" & strBills & "'," & intNoOfRef & ");"

            DoCmd.RunSQL strSQL
        Else
            'Do nothing.
        End If
        DoCmd.SetWarnings True

     '@
                
                
                'strSQL = "INSERT INTO WebHits (WebPageHit,NoOfRef) VALUES ('" & strBills & "'," & intNoOfRef & ");"
            DoCmd.SetWarnings True
    
    
    
    'Send the page to the supervisor for that section and/or other people.
         
         
         'strAmendments = "[URL unfurl="true"]http://www.google.com/search?as_q=[/URL] " & rst!keyword & Chr$(34) = "en&num=100&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&cr=&as_ft=
i&as_filetype=&as_qdr=y&as_nlo=&as_nhi=&as_occt=any&as_dt=i&as_sitesearch=
http%3A%2F%2F[URL unfurl="true"]www.leg.state.nv.us%2F74th%2Fbills%2FAmendments%2F&as_rights=[/URL]
&safe=active"
        'Debug.Print strAmendments
        'Debug.Print strBills
        'Application.FollowHyperlink strAmendments, newwindow:=True, addHistory:=False
        'Application.FollowHyperlink strBills, newwindow:=True, addHistory:=False
    rst.MoveNext
    Loop
    DoCmd.SetWarnings True
    rst.Close
    cn.Close
Exit Function
err_QueryGoogle1:
Select Case Err.Number
    Case 3075 'There is a single quote embedded into the data being input.
        strBills = AllowApostrophes(strBills)
    Case 3129 'Invalid SQL Statement
        MsgBox "Invalid SqlStatement: - " & strSQL
        Resume Next
    Case Else
        ErrBox "Query google"
End Select
End Function

I'm also including the code for the two functions that were lifted verbatim from 'String Things v1.3
Copyright (c) 2003, 2004 Brian J. Payne

Code:
Public Function STCountIf(sSource As String, _
                          sTarget As String, _
                          Optional bCaseSensitive As Boolean = ST_CASESENSITIVE) _
                          As Integer
'Searches the source string for all instances
'   of the target string.
'Returns the count of target strings in the
'   source string.

    Dim iPos As Integer
    Dim iLoop As Integer
    Dim iHits As Integer
    Dim iStrParm As Integer
    
    iPos = 1
    
    iStrParm = STCaseParm(bCaseSensitive)
    
    iLoop = InStr(1, sSource, sTarget, iStrParm)
    
    If iLoop <> 0 Then
        iHits = iHits + 1
        iPos = iLoop + 1
        While iLoop <> 0
            iLoop = InStr(iPos, sSource, sTarget, iStrParm)
            If iLoop <> 0 Then
                iPos = iLoop + 1
                iHits = iHits + 1
            End If
        Wend
    End If

    STCountIf = iHits
                            
End Function

Code:
Private Function STCaseParm(bCaseSensitive As Boolean) As Integer
'Private function to return the proper
'   parameter value for calls to the VB library
    If bCaseSensitive Then
        STCaseParm = vbBinaryCompare
    Else
        STCaseParm = vbTextCompare
    End If

End Function[
 
Sorry - your post is too wide to be viewable.

Sure, the early bird gets the worm, but the second mouse gets the cheese in the trap.
 
Too wide to be viewable? Do you mean physically, or there is too much content?

Alan
 
It won't fit on the screen

Sure, the early bird gets the worm, but the second mouse gets the cheese in the trap.
 
May I ask the Display Resolution of your screen? I can not duplicate the issue. I can read both the question and the code. I have used 800x600 through 1152x864, and it appears readable to me.

I can try reposting at another screen resolution that matches your settings.
 
BTW, I think the issue may be the result of not updating a global variable. I will test that later today.
 
I am entering this text and code at 800x600 resolution. Can you tell me if you can now read it? I have trouble reading it at 640x480 resolution. I can read the code it at every resolution from 800x600 and up I've never had this issue before. Also, if you can see it, I'd appreciate any insights you may have.

Displaying a message box causes a difference in the execution of the following code. See '@ When I run the code with the message box, it properly displays the number of occurrences of the string "ID=", and only writes a line to a table when the condition of the test is met. When I take out the MsgBox, every URL, even those that have "0" occurrences of the "ID=0" are included in the table, and the variable intNoOfRef remains the same instead of having fresh data inserted into it.

Code:
Public Function STCountIf(sSource As String, _
                          sTarget As String, _
                          Optional bCaseSensitive As Boolean = ST_CASESENSITIVE) _
                          As Integer
'Searches the source string for all instances
'   of the target string.
'Returns the count of target strings in the
'   source string.

    Dim iPos As Integer
    Dim iLoop As Integer
    Dim iHits As Integer
    Dim iStrParm As Integer
    
    iPos = 1
    
    iStrParm = STCaseParm(bCaseSensitive)
    
    iLoop = InStr(1, sSource, sTarget, iStrParm)
    
    If iLoop <> 0 Then
        iHits = iHits + 1
        iPos = iLoop + 1
        While iLoop <> 0
            iLoop = InStr(iPos, sSource, sTarget, iStrParm)
            If iLoop <> 0 Then
                iPos = iLoop + 1
                iHits = iHits + 1
            End If
        Wend
    End If

    STCountIf = iHits
                            
End Function

Code:
Public Function STCountIf(sSource As String, _
                          sTarget As String, _
                          Optional bCaseSensitive As Boolean = ST_CASESENSITIVE) _
                          As Integer
'Searches the source string for all instances
'   of the target string.
'Returns the count of target strings in the
'   source string.

    Dim iPos As Integer
    Dim iLoop As Integer
    Dim iHits As Integer
    Dim iStrParm As Integer
    
    iPos = 1
    
    iStrParm = STCaseParm(bCaseSensitive)
    
    iLoop = InStr(1, sSource, sTarget, iStrParm)
    
    If iLoop <> 0 Then
        iHits = iHits + 1
        iPos = iLoop + 1
        While iLoop <> 0
            iLoop = InStr(iPos, sSource, sTarget, iStrParm)
            If iLoop <> 0 Then
                iPos = iLoop + 1
                iHits = iHits + 1
            End If
        Wend
    End If

    STCountIf = iHits
                            
End Function

Code:
Private Function STCaseParm(bCaseSensitive As Boolean) As Integer
'Private function to return the proper
'   parameter value for calls to the VB library
    If bCaseSensitive Then
        STCaseParm = vbBinaryCompare
    Else
        STCaseParm = vbTextCompare
    End If

End Function
 
I also have trouble reading your whole post, but I get the gist and I think I have an idea.

My guess is that you have code in one of the functions you are not showing (perhaps GoToURL) that is not finishing by the time you get to the next line in the main function. I'm guessing that MsgBox, which basically pauses execution of your main subroutine, gives the other code time to "catch up" and finish, which is why it works with the MsgBox.

I'm guessing your variable gstrInnerText is global, and that you are updating it in GoToURL. Also guessing that GoToURL somehow loads the webpage (perhaps through a browser control, or something similar), which is asynchronous with your main code. So perhaps GoToURL (or maybe GetHTML) has not finished loading the web page, and thus has not finished updating gstrInnerText.

Assuming my conjectures are correct, I think once you start downloading the HTML document, you will need to pause the main routine until the document is completely loaded. Whatever method you are using to download the page, it should provide some event that will notify you when it has finished loading. When that event fires, you can resume the main code.

 
Damn, now my post isn't displaying completely!

Copy my text, and paste it into Word or WordPad.

Then you should be able to read it all.


 
Joe,

I gave you a star immediately upon reading your analysis. You were correct. I added some code to check for completion and if not done, to kill some time with a DoWhile loop.

Thanks,
Alan

P.S. Can you read this post okay? I'm not sure why there are issues.
 
I can read your last post OK.

The formatting issue probably has to do with the code you pasted in.

It is not wrapping text so there are parts cut off on the right side.


 
I understand. For the future, I'll cut-and-paste to Notepad, and copy-and-paste from Notepad into the box.

Funnily, I can read everything fine on this computer, and on another computer that I have at home.

Once again, thank you.
 
Actually, now it loads properly. Maybe somebody at Tech Tips fixed the layout.

It might depend on the browser, I use IE7.


 
I've advertised the site management.
The unreadble wide posts are specific to IE since IE6 ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top