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

Type Mismatch Error 1

Status
Not open for further replies.

kphu

MIS
May 30, 2002
346
US
Hi Everyone,

I'm editing this code in notepad so unfortunately, I don't have much tools to work with. I'm getting a type mismatch error and I understand where the problem is but I'm not sure how to fix it. Any help would be much appreciated.

The function below opens up a log file and reads each line for keywords of "Error" or "Warning". If it finds it, then it sends an email. We found that this functions works great except that we run this every 15 minutes to check the log file. We realized that the code will send emails for all "Error" or "Warning" found within the log even on those found 15 minutes ago. So I added some coding based on the structure of the log file. Basically compare the time stamp of each line to the current time and if its within 15 minutes then send the email, if not ignore it.

Here's a sample of the log data in this example assume the time is 10:15. The log captures the data in military time.

2006-05-05 11:21:26.136 : Record Not Found: S7_506 failed scanner: 522 HU: 
.
2006-05-15 9:00:26.136 : Error Should Not Find: S7_506 failed scanner: 522 HU: 
.
2006-05-15 10:05:26.136 : Error Should Find 1: S7_506 failed scanner: 522 HU: 
.
2006-05-15 10:05:26.136 : Error Should Find 2: S7_506 failed scanner: 522 HU: 
.

Code:
Function checkLogs(currFlagShiplog)

   Dim fso, f1, ts, s, notifyString
   Dim hn, mn, hb, mb 'hn = hournow, hb = hour before, etc
   Const ForReading = 1
   notifyString = ""
   Set fso = CreateObject("Scripting.FileSystemObject")
   ' Read the contents of the file.
   Set ts = fso.OpenTextFile(currFlagShiplog, ForReading)
   
   hn = left(right(now,11),2)
   mn = mid(right(now,11),4,2)

  Do While ts.AtEndOfStream <> True

  s = ts.ReadLine
  hb = mid(s,12,2)
  mb = mid(s,15,2)
if (hn - hb) <> 0 then 
'[COLOR=#ff0000] Type mismatched error with hn - hb because different data type. I've tried dim hn, hb as integer and it won't take it.  [/color]
else
if (mn - mb) >= 15 then
'[COLOR=#ff0000]Same error here[/color]
else 
	if s = "         ." then
	else
   	if Instr(s,"Warning") <> 0 or Instr(S, "Error") <> 0 then
		
		notifyString = s
	else	
   	End if
	End if

   If notifyString <> "" Then
	notifyIT(notifyString)
  notifyString = ""
   Else

   End if
end if
end if

  Loop
   
   ts.Close

   Set ts = nothing
   Set fso = nothing

End Function
 
The date time formatting is the problem.

[tt][blue]
2006-05-05 [red]11[/red]:21:26.136
2006-05-15 [red]9:[/red]00:26.136
2006-05-15 [red]10[/red]:05:26.136
2006-05-15 [red]10[/red]:05:26.136
[/blue][/tt]

You have...

hb = mid(s,12,2)
mb = mid(s,15,2)

So, you expect the hour to start at the 12'th position and be 2 characters wide. When the hour (on the second line of the example) is 1 digit long, you get a problem because everything is shifted to the left by 1 character.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks George.

I actually figured out the problem, but what you found was part of it.

For that you get a star. Here's the final product.

Code:
Function checkLogs(currFlagShiplog)

   Dim fso, f1, ts, s, notifyString
   
   Const ForReading = 1
   notifyString = ""
   Set fso = CreateObject("Scripting.FileSystemObject")
   ' Read the contents of the file.
   Set ts = fso.OpenTextFile(currFlagShiplog, ForReading)
   
   hn = hour(now)
   mn = minute(now)
  Do While ts.AtEndOfStream <> True

  s = ts.ReadLine
if s = "         ." then
  else
  hb = int(mid(s,12,2))
  mb = int(mid(s,15,2))
	if (hn - hb) <> 0 then
	else
	if (mn - mb) >= 15 then
	else 
	
   		if Instr(s,"Warning") <> 0 or Instr(S, "Error") <> 0 then
		
		notifyString = s
		else	
   		End if

	   	If notifyString <> "" Then
		notifyIT(notifyString)
		notifyString = ""
   		Else
		End if
	End if
	End if
End if
  Loop
   
   ts.Close

   Set ts = nothing
   Set fso = nothing

End Function
 
Your method for comparing the times is not accurate. You first compare the hours and then you compare the minutes. If you checked at 10:01 AM and there was a log entry from 9:59 AM, it wouldn't get processed because the hours are different, eventhough it's only 2 minutes apart. Instead of splitting the time on hours and minutes, I suggest you combine the date in to an actual date variable.

Code:
Function checkLogs(currFlagShiplog)

   Dim fso, f1, ts, s, notifyString
   Dim dteLogDate
   
   Const ForReading = 1
   notifyString = ""
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set ts = fso.OpenTextFile(currFlagShiplog, ForReading)
   
   Do While ts.AtEndOfStream <> True
        s = ts.ReadLine
        If Trim(s) <> "." Then
            dteLogDate = Split(s, ".")(0)
            If DateDiff("n", dteLogDate, Now) < 15 Then
                If InStr(s, "Warning") <> 0 Or InStr(s, "Error") <> 0 Then
                    notifyString = s
                End If
                
                If notifyString <> "" Then
                    notifyIT (notifyString)
                    notifyString = ""
                End If
            End If
        End If
    Loop
   
   ts.Close

   Set ts = Nothing
   Set fso = Nothing

End Function

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hey George,

After I finished the code and was testing it I caught the issue and was going to work on it today.

I test your code and it works great. Thanks for the help!

Ken
 
No problem. I'm glad you got it working.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I actually added some error control in there. Here's the final product. If you see anything that I missed let me know. Thanks for the help.

Code:
Function checkLogs(currFlagShiplog)

   Dim fso, f1, ts, s, s1, notifyString
   Dim dteLogDate
   
   Const ForReading = 1
   notifyString = ""
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set ts = fso.OpenTextFile(currFlagShiplog, ForReading)
   
   Do While ts.AtEndOfStream <> True
        s = ts.ReadLine
	s1 = s
        If Trim(s) <> "." Then
            dteLogDate = Split(s, ".")(0)
	    On Error Resume Next
            If DateDiff("n", dteLogDate, Now) < 15 Then
                If InStr(s, "Warning") <> 0 Or InStr(s, "Error") <> 0 Then
                    notifyString = s
                End If
                
                If notifyString <> "" Then
                    notifyIT (notifyString)
                    notifyString = ""
                End If
	        
                If Err.Number <> 0 Then
		    notifyString = "There was a script error on the LogCheck.vbs script. The vb error code is " & Err.Number & ". " & _
			"This line caused the error. " & s1
                    notifyIT (notifyString)
		    notifyString = ""
                    Err.Clear
		    On Error GoTo 0
		End if
            End If
        End If
    Loop
   
   ts.Close

   Set ts = Nothing
   Set fso = Nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top