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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code help needed

Status
Not open for further replies.

tani1978

MIS
Sep 29, 2005
49
DE
I have a piece of code which searches for a specific word in Modules and then list them. The Problem is the word is not searched and found by this piece of code. Every time 0 numbers of word are searched by this piece of code. How should I modify it that it should search the specific word and list it?I dont know where I am making the mistake....

Code:
Public Sub Command41_Click()
On Error GoTo Err_Command27
    Dim lngCounterA As Long, lngCounterB As Long, lngCounterC As Long
    Dim modModule As Module
    Dim zahl    
    Dim zahl1
    Dim zahl2
    For lngCounterA = 0 To Modules.Count - 1
        Set modModule = Modules.Item(lngCounterA)
        zahl = 0
         
        'Debug.Print "Number of Modules " & lngCounterA & ""
        With modModule
            For lngCounterB = 1 To .CountOfLines
                If Trim(.Lines(lngCounterB, 1)) = "EOF" Then
                   ' .ReplaceLine lngCounterB, "Washington"
                    zahl = zahl + 1
                End If
            Next lngCounterB
         '   Debug.Print "EOF comes in Module" & modModule & "   " & zahl & " times."
            zahl1 = 0
            For lngCounterC = 1 To .CountOfLines
                If Trim(.Lines(lngCounterC, 1)) = "EOF" Then
                   ' .ReplaceLine lngCounterC, "Washington"
                    zahl1 = zahl1 + 1
                End If
            Next lngCounterC
        End With
        'Debug.Print "Recordset comes in " & modModule & "   " & zahl1 & " times."
       Me.Text28 = Me.Text28 & vbNewLine & "EOF comes in Module" & modModule & "   " & zahl1 & " times."
       
        Next lngCounterA
        
Exit_Command27:
    Exit Sub
Err_Command27:
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_Command27
End Sub
 
Have a look to the InStr function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
i have tried to handle this with inStr but it was not giving me the desired results also.
 
I have this problem solved in another way but there i have to give the name of each Module and text to be searched and that is too time taking. I am trying to merge that code in some way in the code written above.Here is the Code which is working.

Code:
Public Function SearchandReplace(strModulname As String, _
                                  strSuchText As String, _
                                  strNeuerText As String) As Boolean
On Error GoTo Error_SuchenUndErsetzen
    Dim mdl As Module
    Dim lngAnfangszeile As Long, lngAnfangsspalte As Long
    Dim lngEndzeile As Long, lngEndspalte As Long
    Dim strzeile As String, strNeueZeile As String
    Dim intChr As Integer, intVorher As Integer, intNachher As Integer
    Dim strLinks As String, strRechts As String

    ' Modul öffnen.
    DoCmd.OpenModule strModulname
    ' Verweis auf Module-Objekt zurückgeben.
    Set mdl = Modules(strModulname)
    ' Zeichenfolge suchen.
    If mdl.Find(strSuchText, lngAnfangszeile, lngAnfangsspalte, lngEndzeile, _
        lngEndspalte) Then
        ' Textzeile speichern, in der der Suchbegriff vorkommt.
        strzeile = mdl.Lines(lngAnfangszeile, _
                             Abs(lngEndzeile - lngAnfangszeile) + 1)
        ' Zeilenlänge ermitteln.
        intChr = Len(strzeile)
        ' Anzahl der Zeichen vor dem Suchbegriff ermitteln.
        intVorher = lngAnfangsspalte - 1
        ' Anzahl der Zeichen hinter dem Suchbegriff ermitteln.
        intNachher = intChr - CInt(lngEndspalte - 1)
        ' Zeichen links vom Suchbegriff speichern.
        strLinks = Left$(strzeile, intVorher)
        ' Zeichen rechts vom Suchbegriff speichern.
        strRechts = Right$(strzeile, intNachher)
        ' Zeichenfolge mit dem Ersatztext erstellen.
        strNeueZeile = strLinks & strNeuerText & strRechts
        ' Ursprüngliche Zeile ersetzen.
        mdl.ReplaceLine lngAnfangszeile, strNeueZeile
        SuchenUndErsetzen = True
      Else
'        MsgBox "Der Text '" & strSuchText & "' wurde nicht gefunden."
        SuchenUndErsetzen = False
    End If

Exit_SuchenUndErsetzen:
    Exit Function
Error_SuchenUndErsetzen:
    MsgBox Err & ": " & Err.Description
    SuchenUndErsetzen = False
    Resume Exit_SuchenUndErsetzen
End Function

Public Function AlleSearchandReplace(strModulname As String, _
                                      strSuchText As String, _
                                      strNeuerText As String) As Boolean
    Dim blnWeiter   As Boolean
    Dim lngAnzahl   As Long
    
    lngAnzahl& = -1
    Do
        blnWeiter = SearchandReplace(strModulname$, strSuchText$, _
                                      strNeuerText$)
        lngAnzahl& = lngAnzahl& + 1
    Loop Until Not blnWeiter
    Select Case lngAnzahl&
      Case 0
        MsgBox "Im Modul [" & strModulname$ & "] the Text '" & _
               strSuchText$ & "' not found!"
      Case Else
        MsgBox "Im Modul [" & strModulname$ & "] was the text '" & _
               strSuchText$ & "' " & lngAnzahl& & "-mal found and " & _
               "durch den Text '" & strNeuerText$ & "' replaced!"
    End Select
    lngAnzahl& = lngAnzahl& - 1
End Function
 
I have tried to do with InStr but I get an error. Here is my Code.The Red lines are making problem in the code.Any Suggestion from you people.
Code:
Public Sub Command41_Click()
On Error GoTo Err_Command27
    Dim lngCounterA As Long, lngCounterB As Long, lngCounterC As Long
    Dim modModule As Module
    Dim zahl    
    Dim zahl1
    Dim zahl2
    For lngCounterA = 0 To Modules.Count - 1
        Set modModule = Modules.Item(lngCounterA)
        zahl = 0
         
        'Debug.Print "Number of Modules " & lngCounterA & ""
        With modModule
            For lngCounterB = 1 To .CountOfLines
               [COLOR=red] If InStr(Trim(.Lines(lngCounterB, 1)) = "EOF" Then [/color]
                   ' .ReplaceLine lngCounterB, "Washington"
                    zahl = zahl + 1
                End If
            Next lngCounterB
         '   Debug.Print "EOF comes in Module" & modModule & "   " & zahl & " times."
            zahl1 = 0
            For lngCounterC = 1 To .CountOfLines
                [COLOR=red] If InStr(Trim(.Lines(lngCounterC, 1)) = "EOF" Then [/color]
                   ' .ReplaceLine lngCounterC, "Washington"
                    zahl1 = zahl1 + 1
                End If
            Next lngCounterC
        End With
        'Debug.Print "Recordset comes in " & modModule & "   " & zahl1 & " times."
       Me.Text28 = Me.Text28 & vbNewLine & "EOF comes in Module" & modModule & "   " & zahl1 & " times."
       
        Next lngCounterA
        
Exit_Command27:
    Exit Sub
Err_Command27:
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_Command27
End Sub
 
Have you even pressed the F1 key to look at the syntax of the InStr function ?
 
Yes I have but I am not a professional VBA programmer
 
A starting point to count the number of lines having "recordset":
For lngCounterC = 1 To .CountOfLines
If InStr(1, .Lines(lngCounterC, 1), "recordset", 1) > 0 Then
zahl1 = zahl1 + 1
End If
Next
Debug.Print "Recordset comes in " & modModule & " in " & zahl1 & " lines."

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top