In Access 2007, I have a form to add a new contact to a table:
RecSet.AddNew
RecSet![Code_Personal] = Me.txtCodePersonal.Value
RecSet![FName] = Me.TxtFName.Value
RecSet![LName] = Me.txtLName.Value
RecSet![Tel Natel] = Me.txtNatTel.Value
RecSet![Tel Home] = Me.txtHomeTel.Value
RecSet![Email] = Me.txtEmail.Value
RecSet.Update
This has worked so far, and the contact has successfully been aded. But I'm having two problems:
I want to display a messagebox to tell the user the contact was successfully added
If the contact was not successfully added because
A contact with this name already exists
A different issue
Then display a message box "Contact already exists" or "error occured" respectively.
My idea of doing this is:
If recSet.Update = true Then
MsgBox "Paolo Bernasconi was successfully added"
Else if RecSet![FName] & RecSet![LName] 'already exist in table
MsgBox "Contact already exists"
Else
MsgBox "An unknown error occured"
I know this code is wrong, and obviously doesn't work, but it's just to give you an idea of what I'm trying to achieve. Thanks for all your help in advance.
Add an error handler to your procedure.
On Error GoTo ErrorHandler
Then display the "success" notice to user immediately after updating the recordset.
RecSet.Update
MsgBox RecSet![FName] & " " & RecSet![FName] & _
" was successfully added"
If the update attempt fails, flow control passes to the ErrorHandler section.
ErrorHandler:
MsgBox "Oops!"
Undoubtedly you want something more refined than an "Oops!" message. A slick approach is to use a Select Case block to customize the response based on error number.
Determine whether the contact exists already before attempting to add it.
strCriteria = "Fname = '" & RecSet![FName] & "' AND LName = '" & _
RecSet![LName] & "'"
Debug.Print strCriteria
If DCount("*", "YourTable", strCriteria) > 0 Then
' do not attempt to add it again
MsgBox "Contact already exists"
Else
RecSet.AddNew
' and so forth
End If
Check the Debug.Print output in case I made a mistake when building strCriteria.
The intention here is to avoid the duplication error condition ... by only attempting to add a contact which doesn't exist. So that error should not happen, and any other errors will be dealt with by the error handler.
Related
This is driving my crazy. I get an error ("500 - Internal server error.") on my web page when returning from this sub. It executes just fine and does what it's supposed to do: add a record of a user (MemID) to the Chat table for an event (EventID) if it hasn't already been added. The first query is to find out if there's already a record. If not, the INSERT statement adds a record. The error occurs after the sub has run.
Sub NewView (EventID)
MemID=Session("MemID")
If MemID>0 then
Set cn5=Server.CreateObject("ADODB.connection")
cn5.open application("gbConnect")
SQL="SELECT Chat.MemID, Chat.EventID FROM Chat WHERE Chat.MemID=" & MemID & " AND Chat.EventID=" & EventID & ";"
cn5.cursorLocation=3
Set Rst=cn5.execute(SQL)
If Rst.recordcount = 0 then
Comment="is watching"
SQL="INSERT INTO Chat ( MemID, EventID, Comment ) SELECT " & MemID & "," & EventID & ", '" & Comment & "';"
cn5.execute(SQL)
Set cn5=nothing
End If
End If
End Sub
On the page:
NewView (EventID) 'Returning from this sub causes an error! "500 - Internal server error."
OK. I figured it out. I got a lesson in scope of variables. Setting the variable Rst (recordset) in the sub ruined the same variable on the main page. Now just have to wait for my hair to grow back.
I'm making some changes to a program that was written by another developer, which uses an SSDB Grid.
I'm writing the code for the BeforeUpdate method.
On Error GoTo BeforeUpdate_Err
Dim ans%
ans% = MsgBox("These changes will be committed to the database. These changes cannot be undone. " & _
"Would you like to continue?", vbYesNo, "Confirm Changes")
If ans% = 7 Then
Grd_Collection.CancelUpdate
End If
Exit Sub
BeforeUpdate_Err:
MsgBox (Err.Description)
The only other code for the grid is the InitColumnProps method.
However, after hitting the Exit Sub line, I get an error message "Update Error".
I've searched the code for this being hard-coded but it isn't, so it's coming from the grid.
What is causing the error and how do I fix it?
Doesn't the BeforeUpdate method pass in an integer? (Cancel As Integer) or something?
Therefore, you should just be able to change your code (and tidy it up) to this:
On Error GoTo BeforeUpdate_Err
If MsgBox("These changes will be committed to the database. These changes cannot be undone. " & _
"Would you like to continue?", vbYesNo, "Confirm Changes") = vbNo Then
Cancel = 1
End If
Exit Sub
BeforeUpdate_Err:
MsgBox (Err.Description)
Set ParentObject=Browser("Title:=Sign-In").Page("Title:=Sign-In Home Page")
PropertyRequired="name"
PropertyValue="Agree and Login"
Result="yes"
Function WebButton(ParentObject,PropertyRequired,PropertyValue,Result)
Call ReportResult(Result)
Dim hit
hit=0
If PropertyRequired="" then
PropertyRequired="name"
End If
If ParentObject.WebButton(PropertyRequired &":="&PropertyValue).exist then
ParentObject.WebButton(PropertyRequired &":="&PropertyValue).click
hit=1
End if
If hit>0 then
Reporter.reportevent micpass,"The button: "&PropertyValue,"Clicked sucessfully"
else
Reporter.reportevent micpass,"The button: "&PropertyValue,"not Clicked sucessfully"
End if
End Function
After I Call this function, then an error " Wrong number of arguments or invalid property assignments" displayed.
Call WebButton(ParentObject,PropertyRequired,PropertyValue,Result)
I just executed your piece of code .I dont find any issues .
Try using parenthesis when calling Reporter.reportevent so it's like this:
Reporter.reportevent (micpass, "The button: " & PropertyValue, "Clicked sucessfully")
Although I found similar problems none could provide a sufficient answer to my problem.
I came across an older VBScript that is used for communication and synchronization with Outlook. Somebody (not me) gets a strange error when running the script:
Script:
"Some path"
Line: 286
Character: 9
Error: Invalid process or invalid argument
Code: 800A0005
Source: Runtime Error in VBScript
Now what I want to know is WHY that error occurs. Just a general interpretation of the error. But first here is the relevant code:
Set refnum = open_outputfile(outputfileverz & outputfilename_short)
If (errcode = 0) Then
If (check_textfile(refnum)) Then
refnum.WriteLine (strOutput)
errcode = close_outputfile(outputfileverz & outputfilename_short, outputfilename_short, refnum)
If (errcode = 0) Then
If (logging) Then 'mit Protokollierung
If (check_textfile(logrefnum)) Then
logrefnum.WriteLine ("--- " & Now() & " ---" & crlf & outputfilename_short & ":" & crlf & strOutput)
If (gBAbort) Then
logrefnum.WriteLine (errtext(33)) ' Abbruch durch den Benutzer
End if
End If
End If
Else
ok = false
End If
Else
'errdetails = outputfileverz & outputfilename_short wurde schon in open_outputfile erstellt
ok = False
End If
End If
Line 286 is:
refnum.WriteLine (strOutput)
There are of course two other functions involved (open_outputfile/check_textfile) but they only return a document reference and check if it is a valid reference respectively.
Now I tried to reproduce the error and there is a problem: No matter what I do I don't get this error. I've tried empty references, empty strings, wrote all kind of variable types but I always get other errors.
Can anybody tell me how or why this happens?
I want to get the email address of the unread mail which is from a particular sender.i tried the following code but it did'nt work
Set olApp=CreateObject("Outlook.Application")
Set olMAPI=olApp.GetNameSpace("MAPI")
Set oFolder = olMAPI.GetDefaultFolder(6)
Set allEmails = oFolder.Items
For Each email In oFolder.Items
If email.Unread = True Then
If email.SenderEmailAddress="Kalyanam.Raghuram#xxxx.com" Then
MsgBox email.Subject
End If
End If
Next
so i checked what actually 'email.SenderEmailAddress' is verifying with then by inserting this code
For Each email In oFolder.Items
If email.Unread = True Then
MsgBox email.Subject
MsgBox email.SenderEmailAddress
End If
Next
it gave me some output which cannot be understood but readable.Please let me know any solution for it.
Dio you mean you got back an EX type address instead of the expected SMTP?
Have you looked at the _ExchangeUser.PrimarySmtpAddress?
In your case you can use MailItem.Sender.GetExchangeUser.PrimarySmtpAddress. Be prepared to handle nulls as each value can be null.
The code you posted worked for me, I am on Windows Vista with Outlook 2007
One thing I would change is this
If LCase(email.SenderEmailAddress) = LCase("Kalyanam.Raghuram#xxxx.com") Then
wscript.echo email.Subject
End If