Dlookup string failing - ms-access-2013

For some reason i can't get the dlookup to function correctly i keep getting errors. The error for this is data type mismatch.
Option Compare Database
Private Sub Command131_Click()
DoCmd.SetWarnings False
If (Nz(DLookup("LetterSent1Bool", "dbo_T_Volunteers", _
"VolunteerID = '" & Me![VolunteerID] & "'"))) > 0 Then
MsgBox "ERROR ! This Volunteer has already received this Letter ,"
Else
DoCmd.OpenQuery "ProduceLettersSixToTwelve", , acReadOnly
If DCount("*", "dbo_T_SixToTwelveWeeks") > 0 Then
MsgBox "SUCCESS ! Please Open The Mail Merge Template"
Else
MsgBox "ERROR ! No Records Found"
End If
End If
End Sub

Figured it out, i need to remove quotes around & Me![VolunteerID]
Option Compare Database
Private Sub Command131_Click()
DoCmd.SetWarnings False
If (Nz(DLookup("LetterSent1Bool", "dbo_T_Volunteers", _
"VolunteerID = " & Me![VolunteerID]))) > 0 Then
MsgBox "ERROR ! This Volunteer has already received this Letter ,"
Else
DoCmd.OpenQuery "ProduceLettersSixToTwelve", , acReadOnly
If DCount("*", "dbo_T_SixToTwelveWeeks") > 0 Then
MsgBox "SUCCESS ! Please Open The Mail Merge Template"
Else
MsgBox "ERROR ! No Records Found"
End If
End If
End Sub

Related

VbScript Error Object Cleared by On Error Statement

Kind of a novice with VbScript, and trying to implement error handling. My method is to pass the error object to a HandleErr sub, but the error apparently gets cleared by the "On Error Resume Next" statement withing the sub. Using Windows 7.
On Error Resume Next
Dim x
x = 1/0
msgbox "Original Error: " & err.Number & " - " & err.Description
if err.number <> 0 then HandleErr err
Sub HandleErr(objErr)
on error resume next '### Without this On Error statement, the script runs fine.
msgbox "Error in HandleErr: " & objErr.Number & " - " & objErr.Description '### objErr.Number becomes zero.
WScript.Quit objErr.Number
End Sub
I imagine there is a simple answer for this. Any help would be greatly appreciated.
You want to stop the skipping errors with On Error Resume Next once you reach HandleErr(). Also use Err.Clear() to reset Err object.
On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
if Err.Number <> 0 then HandleErr Err
'Stop skipping lines when errors occur.
On Error Goto 0
Sub HandleErr(objErr)
MsgBox "Error in HandleErr: " & objErr.Number & " - " & objErr.Description '### objErr.Number becomes zero.
'Clear current error now you have trapped it.
Err.Clear
WScript.Quit objErr.Number
End Sub
Personally though I wouldn't pass Err into your function because Err is a global built-in object so you can still check the values without passing it in.
On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
Call HandleErr()
'Stop skipping lines when errors occur.
On Error Goto 0
Sub HandleErr()
'Do we need to trap an error?
If Err.Number <> 0 Then
MsgBox "Error in HandleErr: " & Err.Number & " - " & Err.Description '### Err.Number becomes zero.
'Clear current error now you have trapped it.
Err.Clear
WScript.Quit Err.Number
End If
End Sub

Issues in Error handling sub in VB Script

I have written an error handling sub in my vb script
errorNumber = DoAllWork
Sub ErrorHandling (Number, Description, i)
If Number <> 0 Then
WriteLogFileLine logfile, "Error No : " & Number & " - " & Description & " has occurred !"
Else
WriteLogFileLine logfile, "Success copying files as Err.Number : " & Err.Number & "Total " & i & " files were copied ! " & vbcrlf
End If
Err.Clear
End Sub
And I am calling it in my vb script like this
Function DoAllWork
On Error Resume Next
Err.Clear
Do Until CopyFiles.AtEndOfStream
line = CopyFiles.ReadLine
For Each line In CopyFiles
If objFSO.GetFolder(line).Files.Count <> 0 then
WriteLogFileLine logfile, "Copying files FromLocation " & Chr(34) & line & Chr(34) & " to ToLocation " & Chr(34) & ToLocation & Chr(34)
Else
WriteLogFileLine logfile, "No files present in the folder " & Chr(34) & line & Chr(34) & vbcrlf
End if
i=0
For Each File In objFSO.GetFolder(line).Files
objFSO.GetFile(File).Copy ToLocation & "\" & objFSO.GetFileName(File),True
i=i+1
Next
ErrorHandling Err.Number, Err.Description, i
Next
Loop
End Function
Now the log file which is getting created has this error messages logged in it even though the files has got copied successfully. Can someone please suggest what is wrong with this error handling technique ??
2015-12-15 15:03:47 - Copying files FromLocation "\\srv10219\archive\Article\20151116_073104" to ToLocation "C:\Users\TEMPPAHIR\LearnVB\ICCdata\Article"
2015-12-15 15:03:47 - Error No : 438 - Object doesn't support this property or method has occurred !
when I place this error handling directly after the File.copy statement, it gives me such log..
2015-12-15 16:31:55 - Error No : 438 - Object doesn't support this property or method has occurred !
2015-12-15 16:31:55 - Success copying files as Err.Number : 0
2015-12-15 16:31:55 - Total 2 files were copied !
that means for the first file which is being copied it throws an error and for the second one it gives success even though both the files has been copied successfully
I implemented this to do pretty much what you asked. What the script needed to do was take a folder and its contents that was dropped into a directory, and then copy the contents to a new folder formatted correctly where there was a poller that picked up the files and entered them in our system. I needed the script to quit on any error, especially for the last thing to do. That was to delete the folder from the source directory, but I had to make sure it got copied correctly. (You could clear the error and continue as well.)
So after each important line like a folder create or a file copy I did this check. This worked for me to do error handling. Passing the Err object itself is the best way to go.
Set FolderCreate = FSO.CreateFolder(PreStagingDirectory + "\" + FolderCreateName)
ReportErrors Err,"Error creating folder: " + PreStagingDirectory + "\" + FolderCreateName
FSO.CopyFile objFile.Path, FolderCreate.Path + "\", true
ReportErrors Err,"Error copying file: " + objFile.Path + " to location: " + FolderCreate.Path
Sub ReportErrors(ErrorObject, strExtraInformation)
'This will log any errors if they happen and the script will then quit
If ErrorObject.Number <> 0 Then
OutPutFile.WriteLine(DateTimeString + " Error Number: " & ErrorObject.Number)
OutPutFile.WriteLine(DateTimeString + " Error (Hex): " & Hex(ErrorObject.Number))
OutPutFile.WriteLine(DateTimeString + " Source: " & ErrorObject.Source)
OutPutFile.WriteLine(DateTimeString + " Description: " & ErrorObject.Description)
OutPutFile.WriteLine(DateTimeString + " Other Information: " + strExtraInformation)
OutPutFile.WriteLine(DateTimeString + " Script is quitting due to the Error Condition.")
wscript.quit
End If
End Sub
I have also done code like this. In this case I needed to catch if null was returned from a SQL query to the database. If null was returned and I try to cast the value, it caused an error.
To answer the question of should you check for errors in a sub and clear them, or check for errors all through your script really depends what you need to accomplish.
First I have to say error handling in vbscipt just sucks. Using 'On Error Resume Next', is absolutely needed but be careful where you place it. I found it is not best to place it in main, at the top, just within each function. Most important is to remember the scope, if you put it in a function, you catch the error there, and can handle it. If you only have it at as the first line of your script, you can only handle an error in main. It is needed in main, sometimes.
Also to debug any vbscript, comment out any call to that, or you will never know the problem.
ExecutionString = "select Sum(cast(Frame_Count as int)) from Sop_Instance_T"
Set objRecordSet = objConnection.Execute(ExecutionString)
TotalFrames = cdbl(objRecordSet(0))
'In case null is returned catch the exception
'that happens on the above line
If Err.Number <> 0 Then
TotalFrames = 0
Err.Clear ()
End If

VbScript make computer say something error

I have this file that makes a computer say something. I want it to loop with a VbCancel function. I get this error. Code so far:
Do
Dim Message, Speak
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Speak)
Speak.Speak Message
If Len(Speak) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Sub
End If
Loop
BTW the error is Invalid exit statement
I'm working on Windows 7
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop
You had several issues here
Exit Sub is for subroutines. You were trying to exit a Do loop
Speak is an object. I dont know if it has a string property but it is not itself a string. Both Len(Speak) and "You entered: " & Speak has Speak changed to Message.
You were missing an End If
I moved the Dim statements out of the loop. No point recreating the object over and over again.
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop

Winsock error 40009 vb6

Winsock1.Connect "mail.website.com", 110
Do Until received: DoEvents: Loop
If sckError Then MsgBox "An error occured trying to connect to server": Exit Sub
sendMsg "USER username" ' Send UserName
If sckError Then MsgBox "Error with username": Exit Sub
sendMsg "PASS password" ' Send Password
If sckError Then MsgBox "Error with password": Exit Sub
' Get Number of Messages and total size in bytes
sendMsg "STAT"
x = InStr(Message$, " "): b = InStrRev(Message$, " ")
messages = Val(Mid$(Message$, x + 1, b - x))
Size = Val(Mid$(Message$, b + 1))
If messages = "0" Then
MsgBox "no new messages"
GoTo l
End If
For a = 1 To messages
Winsock1.Tag = "RETR"
Open "C:\Windows\Temp\eMail-" & a & ".eml" For Binary Access Write As #1
sendMsg "RETR " & a
List1.AddItem "eMail-" & a & ".eml"
Next
Winsock1.Tag = ""
Next
l:
Winsock.Close
When i run this the first time it works perfectly, but when i try to run it a second time w/o closing the app, it gives me the 40009 error. I'm thinking im trying to send data before its connected. is there a way to see if winsock is connected? somthing like
if winsock1.state = true then...
You didn't list all of the code, but I'm guessing that the variable received is not reset, causing you to fall through directly to the sendMsg "USER username" code early. You have to let the cinnection actually happen/complete before you can start writing to the socket.

Word macro error messages

I am changing document template macros. The one thing I can't find out how to do is to customize error messages. For example an error message in a document is
"Error! No table of figures entries found"
I would like to change this to display something else. Is it possible to do this with Word VBA or VBScript?
Is it possible to put this in some
kind of global error handler? – Craig
It is possible. Here is a very rough example.
In a standard module:
Sub HandleErr(ErrNo As Long)
Select Case ErrNo
Case vbObjectError + 1024
MsgBox "No table of figures entries found.", vbOKOnly + vbCritical
Case vbObjectError + 1034 To vbObjectError + 4999
MsgBox "Still no table of figures entries found.", vbOKOnly + vbCritical
Case Else
MsgBox "I give up.", vbOKOnly + vbCritical, _
"Application Error"
End Select
End Sub
Some code:
Sub ShowError()
Dim i As Integer
On Error GoTo Proc_Err
'VBA Error
i = "a"
'Custom error
If Dir("C:\Docs\TableFigs.txt") = "" Then
Err.Raise vbObjectError + 1024
End If
Exit_Here:
Exit Sub
Proc_Err:
If Err.Number > vbObjectError And Err.Number < vbObjectError + 9999 Then
HandleErr Err.Number
Else
MsgBox Err.Description
End If
End Sub
If you want to trap a specific error type in VBA, one method is to use On Error Resume Next then test for an error message on the line following the action to trap, e.g.:
On Error Resume Next
' try action
If Err.Number <> 0 Then
' handle w/ custom message
Err.Clear
End If
If you know the exact error number (If Err.Number = N Then), that would be better of course.
Well if you are talking about having a custom message box - that's easy.
Look up 'msgbox' in VBA help for better info.
Msgbox("Error! No table of figures entries found",16,"Error")
The 16 makes it a 'criticial' message.
If you're talking about error trapping then you'll need code like this:
On Error Resume Next
n = 1 / 0 ' this causes an error
If Err.Number <> 0 Then
n = 1
if Err.Number = 1 Then MsgBox Err.Description
End If
When an error is thrown, a number and description are given to the Err object.

Resources