473,415 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,415 software developers and data experts.

Error trapping search form

111 100+
i have the following code that performs a search:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command47_Click()
  2. On Error GoTo Err_Command47_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stDocName = "frmMainGroup"
  8.     If IsNull(Me![GroupNumber]) Then
  9.         MsgBox "Please enter a Group Number.", vbExclamation
  10.     Else
  11.         stLinkCriteria = "[GroupNumber]=" & "'" & Me![GroupNumber] & "'"
  12.         DoCmd.OpenForm stDocName, , , stLinkCriteria
  13.     End If
  14. Exit_Command47_Click:
  15.     Exit Sub
  16.  
  17. Err_Command47_Click:
  18.     MsgBox Err.Description
  19.     Resume Exit_Command47_Click
  20.  
  21. End Sub
I'm trying to figure out a way to error trap it so that if someone puts in a number that doesn't exist, they get a message telling them that it doesnt exist. Right now if they enter a wrong number, it just opens up a blank form.
Aug 23 '07 #1
7 1257
puppydogbuddy
1,923 Expert 1GB
Use a DCount to your number table:
Expand|Select|Wrap|Line Numbers
  1. If DCount("[GroupNumber]", "YourTable", "[GroupNumber] = " & Me![GroupNumber]) = 0 Then 
  2.  
  3. MsgBox "Number does not exist"
  4.  
  5. End If
Aug 23 '07 #2
Neekos
111 100+
Thanks! I see how that would work, but i get a data type mismatch error. Maybe i'm placing it in the wrong spot?

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command47_Click()
  2. On Error GoTo Err_Command47_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stDocName = "frmMainGroup"
  8.     If IsNull(Me![GroupNumber]) Then
  9.         MsgBox "Please enter a Group Number.", vbExclamation
  10.     Else
  11.     If DCount("[GroupNumber]", "tblGroupTool", "[GroupNumber] = " & Me![GroupNumber]) = 0 Then
  12.         MsgBox "Number does not exist"
  13.     Else
  14.         stLinkCriteria = "[GroupNumber]=" & "'" & Me![GroupNumber] & "'"
  15.         DoCmd.OpenForm stDocName, , , stLinkCriteria
  16.     End If
  17.     End If
  18.  
  19. Exit_Command47_Click:
  20.     Exit Sub
  21.  
  22. Err_Command47_Click:
  23.     MsgBox Err.Description
  24.     Resume Exit_Command47_Click
  25.  
  26. End Sub
Aug 23 '07 #3
puppydogbuddy
1,923 Expert 1GB
Thanks! I see how that would work, but i get a data type mismatch error. Maybe i'm placing it in the wrong spot?

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command47_Click()
  2. On Error GoTo Err_Command47_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stDocName = "frmMainGroup"
  8.     If IsNull(Me![GroupNumber]) Then
  9.         MsgBox "Please enter a Group Number.", vbExclamation
  10.     Else
  11.     If DCount("[GroupNumber]", "tblGroupTool", "[GroupNumber] = " & Me![GroupNumber]) = 0 Then
  12.         MsgBox "Number does not exist"
  13.     Else
  14.         stLinkCriteria = "[GroupNumber]=" & "'" & Me![GroupNumber] & "'"
  15.         DoCmd.OpenForm stDocName, , , stLinkCriteria
  16.     End If
  17.     End If
  18.  
  19. Exit_Command47_Click:
  20.     Exit Sub
  21.  
  22. Err_Command47_Click:
  23.     MsgBox Err.Description
  24.     Resume Exit_Command47_Click
  25.  
  26. End Sub
Hi Neekos,

The problem appears to be a syntax problem. I gave you the synatax for a numeric data type, and apparently you are looking up numbers as a text data type.

So try changing this:
If DCount("[GroupNumber]", "tblGroupTool", "[GroupNumber] = " & Me![GroupNumber]) = 0 Then

To this:
If DCount("[GroupNumber]", "tblGroupTool", "[GroupNumber] = '" & Me![GroupNumber] & "'") = 0 Then
__________________________________________________ _____

I noticed that even my text type syntax is different that what you used
as strCriteria ......does your syntax work? where did you get this syntax from? I have not seen the syntax like you have it in strCriteria. Let me know.

Thanks,

pDog
Aug 23 '07 #4
Neekos
111 100+
Thank you! That was the fix!

The reason i have to have to GroupNumber as a text field is because many group numbers are alphanumeric.

And my syntax for the stlinkCriteria does work - i believe i got that from this board somewhere.

I still dont really understand the syntax of it though - i'm not sure when and where to use double quotes or single quotes and the reasoning for it. It drives me nuts! I have a VBA for Access book that i use for reference, but that doesnt really explain it either.
Aug 24 '07 #5
puppydogbuddy
1,923 Expert 1GB
Thank you! That was the fix!

The reason i have to have to GroupNumber as a text field is because many group numbers are alphanumeric.

And my syntax for the stlinkCriteria does work - i believe i got that from this board somewhere.

I still dont really understand the syntax of it though - i'm not sure when and where to use double quotes or single quotes and the reasoning for it. It drives me nuts! I have a VBA for Access book that i use for reference, but that doesnt really explain it either.
Neekos,
Thanks for the feedback. I am glad I was able to help resolve your problem. In regards to the quote syntax, I haven’t found any single reference source either. MS makes learning the rules for quote syntax like potty training…you get rewarded if you get it right…and get nothing if you get it wrong!

All I can tell you is that there is a distinction between a literal string (quotes at the begin and end of the string) and a string that contains a reference to an object or variable that supplies a dynamic value to the string that is not known until runtime. Then there is a further distinction made if the value supplied is numeric or text as you have seen in your case with the groupnumber.
Aug 24 '07 #6
Neekos
111 100+
I do understand it when it's a literal string, but like you said - when its refering to an object within a string - is where i dont get it.

Thanks for your assistance with this. I really appreciate it! This search form is making go nuts. I have it so that they can search by either Group Name, Group Number, or Cruise Line AND sail date. The only problem with Cruise Line and Sail date is: What if there are multiple Groups on the same cruise line and on the same date? I'm not sure how to handle this....right now its just opening the form with the first qualifying group showing.
Aug 24 '07 #7
puppydogbuddy
1,923 Expert 1GB
I do understand it when it's a literal string, but like you said - when its refering to an object within a string - is where i dont get it.

Thanks for your assistance with this. I really appreciate it! This search form is making go nuts. I have it so that they can search by either Group Name, Group Number, or Cruise Line AND sail date. The only problem with Cruise Line and Sail date is: What if there are multiple Groups on the same cruise line and on the same date? I'm not sure how to handle this....right now its just opening the form with the first qualifying group showing.
Neekos,
Ok, understand that in the case where an object is supplying a value at runtime, that object/variable has to be separated out of the string in order for Access to know that there is a value to be returned to the string. Once the value is returned, Access needs to know how to treat it....Is it a numeric value or part of a text string. If the object or variable that is supplying the return value is of the data type that returns a text value to the string, there are an extra set of quotes to designate the return value as text.

example>>>>>>"[GroupNumber] = '" & Me![GroupNumber] & "'"

and if the return is numeric data type, no extra quotes are needed:
for example if GroupNumber were numeric:
>>>>>>"[GroupNumber] = " & Me![GroupNumber]
__________________________________________________ ______________
In reference to your other question, there are several ways to go about it. Off the top of my head (I haven't had the time to test this) , i think this would be my preference:

1. go to your table and create a composite index using the index button in table design view. the index would be on Cruise Line/sail date/groupnumber. This index would be used internally by Access to speed up the search.
2. on your main form, I assume you already capture the Cruise Line/sail date/group number in respective text boxes, so I would place a textbox (e.g. txtCruiseLineIdx) on the form, set its visible property to no, and define its control source as the concatenated value of the 3 controls as shown below. By making the textbox hidden, the search index will be hidden from users.

txtCruiseLineIdx = Me!txtCruiseLineName & Me!txtSailDate & Me!txtGroupNumber

Then do your search Like before

stLinkCriteria = "[txtCruiseLineIdx] = '" & Me!txtCruiseLineIdx & "'"
DoCmd.OpenForm stDocName,,,stLinkCriteria


If for some reason that does not work, try the search this way.
' I believe focus can be set on an invisible textbox as long as control is enabled and not locked.:

Me.txtCruiseLineIdx.SetFocus
DoCmd.FindRecord Me!txtCruiseLineIdx
Me!GroupNumber.SetFocus


Let me know how it goes.
Aug 24 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Steve Richfield | last post by:
My error handler works GREAT. However, VBA seems to have some bugs/features that are causing it fits. The little snippet that I put at the end of each routine looks like this: Error_Handler: If...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
2
by: Bill Stock | last post by:
I have a subform which is causing a 3314 (Field can't contain a null value because required is set to True) error. I solved this problem by trapping it in the before update event. But then I...
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
3
by: Jim Armstrong | last post by:
Hello all - This is driving me crazy. I have a table called tblClients - very simple, has the following fields: taxID (PK) ClientName SalesName The main form of my application allows a...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.