472,982 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Check for Existence of Record THEN Open Form

14
Hello guys, thanks again for taking the time to help me out with my problems!

This problem seems super simple in my head, however getting the coding to make it work is turing out to be not so simple...

Basically what I am trying to do is have a Command Button check to see whether the current "Record ID" exists in the sister table before opening the sister table's bound form for editing when the user clicks on it.

I figured that I would be able to do this with a SELECT query that does a search for the current record's "Record ID" in the sister table, and couple it with an "If Then" statement that will either a) allow access to the form or b) pop up a MsgBox if the results of the SELECT query are Null or Empty and disallow access.

From my research the code below should work, but Access keeps getting hung up on the "Dim db As Database" line, and I cannot for the life of me figure it out!


Expand|Select|Wrap|Line Numbers
  1. Private Sub GoToCertifiedBUTTON_Click()
  2. On Error GoTo Err_GoToCertifiedBUTTON_Click
  3.  
  4.  
  5.  
  6. Dim VerifyExists As String
  7.     VerifyExists = "SELECT tbl_VEH4b.AppID " & _
  8.                           "FROM tbl_VEH4a LEFT JOIN tbl_VEH4b ON tbl_VEH4a.ID = tbl_VEH4b.AppID " & _
  9.                     "WHERE (([Forms]![frm_VEH4a].[ID]=[AppID]));"
  10.  
  11. Dim db As Database
  12. Dim rs As Recordset
  13. Dim qdf As QueryDef
  14.  
  15. Set db = CurrentDb
  16. Set qdf = db.QueryDefs("VerifyExists")
  17. Set rs = qdf.openrecordset()
  18.  
  19. If rs.EOF Then
  20.     Dim stDocName As String
  21.     Dim stLinkCriteria As String
  22.  
  23.     stDocName = "frm_VEH4b"
  24.  
  25.     stLinkCriteria = "[AppID]=" & Me![ID]
  26.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  27.  
  28. Else
  29.     'Open MsgBox with "Cannot Allow" Message
  30. End If
  31.  
  32.  
  33.  
  34. Exit_GoToCertifiedBUTTON_Click:
  35. Exit Sub
  36.  
  37. Err_GoToCertifiedBUTTON_Click:
  38.     MsgBox Err.Description
  39.     Resume Exit_GoToCertifiedBUTTON_Click
  40.  
  41. End Sub
Aug 23 '07 #1
10 5458
JKing
1,206 Expert 1GB
Check that you have a reference to Microsoft DAO Object Library. Also when declaring data objects you should use the full reference to a DAO or ADO object library.

Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. DIm rs as DAO.Recordset
  3.  
Aug 23 '07 #2
bluray
14
Check that you have a reference to Microsoft DAO Object Library. Also when declaring data objects you should use the full reference to a DAO or ADO object library.

Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. DIm rs as DAO.Recordset
  3.  
tried

Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. DIm rs as DAO.Recordset
and

Expand|Select|Wrap|Line Numbers
  1. Dim db as ADO.Database
  2. DIm rs as ADO.Recordset
and it is still having issues.

are "Database" and "Recordset" supposed to be default objects in these libraries? Or do I have to define them somewhere?
Aug 23 '07 #3
JKing
1,206 Expert 1GB
Sorry I should have been more clear in my previous post about checking your reference.

You need to go to Tools > References from the VBA editor window and ensure that you have included the Microsoft DAO Object Library.
Aug 23 '07 #4
bluray
14
Sorry I should have been more clear in my previous post about checking your reference.

You need to go to Tools > References from the VBA editor window and ensure that you have included the Microsoft DAO Object Library.
Thanks JKing, it looks like were on to something here!

I no longer am being sent back to the editor now, but Access is not recognizing the the query when I reference in in the

Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDefs("VerifyExists")
line. It is giving me an "Item not found in this collection" error. Would it work if I just planted the SQL query directly into the brackets?

For example:

Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDefs("SELECT tbl_VEH4b.AppID " & _
  2.                     "FROM tbl_VEH4a LEFT JOIN tbl_VEH4b ON tbl_VEH4a.ID = tbl_VEH4b.AppID " & _
  3.                     "WHERE (([Forms]![frm_VEH4a].[ID]=[AppID]));")
  4.  
Or maybe I am way out to lunch on that...?
Aug 23 '07 #5
missinglinq
3,532 Expert 2GB
What not just use the DCount() function against the sister table to see if the RecordID exists there? If RecordID is a text field, something like:

Expand|Select|Wrap|Line Numbers
  1. If DCount("[RecordID]", "SisterTable", "[RecordID]='" & Me![RecordID] & "'") > 0 Then
  2.   'Open SisterTable here  to the desired record 
  3. Else
  4.  Msgbox "This Record ID does not exist in SisterTable!"
  5. End If
  6. End Sub
Linq ;0)>
Aug 24 '07 #6
JKing
1,206 Expert 1GB
Thanks JKing, it looks like were on to something here!

I no longer am being sent back to the editor now, but Access is not recognizing the the query when I reference in in the

Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDefs("VerifyExists")
line. It is giving me an "Item not found in this collection" error. Would it work if I just planted the SQL query directly into the brackets?

For example:

Expand|Select|Wrap|Line Numbers
  1. Set qdf = db.QueryDefs("SELECT tbl_VEH4b.AppID " & _
  2.                     "FROM tbl_VEH4a LEFT JOIN tbl_VEH4b ON tbl_VEH4a.ID = tbl_VEH4b.AppID " & _
  3.                     "WHERE (([Forms]![frm_VEH4a].[ID]=[AppID]));")
  4.  
Or maybe I am way out to lunch on that...?
QueryDefs is a collection of saved queries in the database. So you can only reference saved queries and not SQL strings. However you can alter a querydef's SQL as well as create new querydefs using an SQL string.

Be sure to have a look at Linq's posted above as he's provided a simple solution.
Aug 24 '07 #7
bluray
14
What not just use the DCount() function against the sister table to see if the RecordID exists there? If RecordID is a text field, something like:

Expand|Select|Wrap|Line Numbers
  1. If DCount("[RecordID]", "SisterTable", "[RecordID]='" & Me![RecordID] & "'") > 0 Then
  2.   'Open SisterTable here  to the desired record 
  3. Else
  4.  Msgbox "This Record ID does not exist in SisterTable!"
  5. End If
  6. End Sub
Linq ;0)>
After hours of finicking I finally got it to work :D Thanks missinglinq and JKing! Here is the finished code:

Expand|Select|Wrap|Line Numbers
  1.         If DCount("[AppID]", "tbl_VEH4b", "Forms![frm_VEH4a]![AppID]") > 0 Then
  2.  
  3.             Dim stDocName As String
  4.             Dim stLinkCriteria As String
  5.  
  6.             stDocName = "frm_VEH4b"
  7.  
  8.             stLinkCriteria = "[AppID]=" & Me![AppID]
  9.             DoCmd.OpenForm stDocName, , , stLinkCriteria
  10.         Else
  11.  
  12.          MsgBox "This applicant has not yet been certified!", vbOKOnly + vbExclamation, "Record Not Found"
  13.  
  14.         End If
Turns out missinglinq that Access didnt like the
Expand|Select|Wrap|Line Numbers
  1. "[RecordID]='" & Me![RecordID] & "'"
part of the DCount. I kept getting a Data Type Mismatch error, so I swapped it for
Expand|Select|Wrap|Line Numbers
  1. "Forms![frm_VEH4a]![AppID]"
and I dont seem to be having any problems thus far. Switches to the sister form without a hitch. Thanks again missinglinq!

QueryDefs is a collection of saved queries in the database. So you can only reference saved queries and not SQL strings. However you can alter a querydef's SQL as well as create new querydefs using an SQL string.
On another note (for educational purposesmore or less) JKing, when you say QueryDefs is a collection of saved queries, where exactly are they saved? In the standard access query area? Or can these queries be coded in SQL within the form and referenced in QueryDes like I was trying to do before (as seen in the code below)?

Expand|Select|Wrap|Line Numbers
  1. Dim VerifyExists As String
  2.     VerifyExists = "SELECT tbl_VEH4b.AppID " & _
  3.                           "FROM tbl_VEH4a LEFT JOIN tbl_VEH4b ON tbl_VEH4a.ID = tbl_VEH4b.AppID " & _
  4.                     "WHERE (([Forms]![frm_VEH4a].[ID]=[AppID]));"
  5.  
  6. Dim db As Database
  7. Dim rs As Recordset
  8. Dim qdf As QueryDef
  9.  
  10. Set db = CurrentDb
  11. Set qdf = db.QueryDefs("VerifyExists")
  12. Set rs = qdf.openrecordset()
Aug 24 '07 #8
JKing
1,206 Expert 1GB
Any queries you create and save within the database are part of this collection. You can programmatically create saved queries using the createquerydef method. Open the helpfile on querydef and read through it. It explains it better than I can.
Aug 24 '07 #9
bluray
14
Any queries you create and save within the database are part of this collection. You can programmatically create saved queries using the createquerydef method. Open the helpfile on querydef and read through it. It explains it better than I can.
great, thanks JKing, thats all I need!

Have a good day!
Aug 24 '07 #10
missinglinq
3,532 Expert 2GB
Sorry, your ID "number" is actually a number! I only use numeric datatypes for fields that are actually going to be used as numbers, i.e. for mathematical calculations!

Glad you got it working!

Linq ;0)>
Aug 24 '07 #11

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

Similar topics

15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
2
by: aaj | last post by:
Hi all I have a continuous bound form and on each record is a tick box. The user ticks the boxes and these boxes define the batch. for future operations before they leave the page I count...
7
by: Shaldaman | last post by:
Hi Is there a property in MS Access for the following: 1) For a Command Button on a form, is there a property that can be used to determine if it has been clicked? eg: Me!button7.Clicked - I...
30
by: S. van Beek | last post by:
Dear reader A record set can be empty because the condition in the query delivers no records. Is there a VBA code to check the status of a record set, record set empty
2
by: www.MessageMazes.com | last post by:
Greetings, I'm experimenting with an ASP page that reads data from a file name that is passed to it as a parameter, as in this page, which works, because the "good" file exists. ...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
25
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden"...
2
by: amuth25 | last post by:
I have a form which displays records based on filter. I need the user to select a record or multiple records by clicking a check box and the selected record details should be displayed in a new form....
1
by: rdsandy | last post by:
Hi, Im using Access 2003. I have a form, with two buttons and a list box. When a user selects an item in the list box, the user clicks on one of the buttons which opens up the selected record. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.