473,396 Members | 1,738 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,396 software developers and data experts.

How do you reference a record within a table in Access using VBA?

Hi,

I have a series of small tables in Access, with table names in the following format ABCYYYYMMDD (eg: ABC20080124). I also have a table named tblMaster which has a date field.

I'm writing a VBA routine where if the date of the small table already existed in tblMaster, the small table is deleted.

I need help with debugging the following code:

Sub CheckDate()

Dim strSQL As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim tdf As DAO.TableDef
Set db = CurrentDb

For Each tdf In db.TableDefs

If Right(tdf.Name, 2) & "/" & Mid(tdf.Name, 8, 2) & "/" & Mid(tdf.Name, 4, 4) = tblMaster.Date

Then


strSQL = "DROP TABLE " & tdf.Name
db.Execute (strSQL), dbFailOnError
Jan 24 '08 #1
6 1733
missinglinq
3,532 Expert 2GB
First off, your post would indicate that you have a field name Date. If this is true, you have to change this to something else! Date is a Reserved Word in Access and a dangerous one! Change it to something like TDate for table date. For an in depth explanation look here.

Next, your code, using the table name ABC20080124 will compare your date field to 24/01/2008. Is this the format of your date field in the tblMaster table?

Welcome to TheScripts!

Linq ;0)>
Jan 25 '08 #2
Hi Linq,

Thank you so much for your help. I've changed the field names in both the series of small tables and tblMaster from Date to TDate. And yes, you are right, the format of the TDate field is 24/01/2008 (ie Short Date).

However, I still can't get the code to work. Any help would be greatly appreciated!

Thanks a lot!
MGN


First off, your post would indicate that you have a field name Date. If this is true, you have to change this to something else! Date is a Reserved Word in Access and a dangerous one! Change it to something like TDate for table date. For an in depth explanation look here.

Next, your code, using the table name ABC20080124 will compare your date field to 24/01/2008. Is this the format of your date field in the tblMaster table?

Welcome to TheScripts!

Linq ;0)>
Jan 27 '08 #3
TerryDM
14
Hi,

I have a series of small tables in Access, with table names in the following format ABCYYYYMMDD (eg: ABC20080124). I also have a table named tblMaster which has a date field.

I'm writing a VBA routine where if the date of the small table already existed in tblMaster, the small table is deleted.

I need help with debugging the following code:

Sub CheckDate()

Dim strSQL As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim tdf As DAO.TableDef
Set db = CurrentDb

For Each tdf In db.TableDefs

If Right(tdf.Name, 2) & "/" & Mid(tdf.Name, 8, 2) & "/" & Mid(tdf.Name, 4, 4) = tblMaster.Date

Then


strSQL = "DROP TABLE " & tdf.Name
db.Execute (strSQL), dbFailOnError
I'ld do it this way on the 'If' just to eliminate any conversion problems:

If Right(tdf.Name,8) = Year(tblMaster.Date) & Month(tblMaster.Date) & Day(tblMaster.Date) then

This way it is 2 strings being compared.

Also have you looked at the input to the If in a MSgBox? How does tblMaster.Date get set - there's nothing in the Sub?
Jan 27 '08 #4
Hi Terry,

My major problem is I don't know how to define the values of the field named "Date" in tblMaster using VBA in Access. Is it something like


Sub CheckDate()

Dim strSQL As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim tdf As DAO.TableDef
Dim table As DAO.TableDef

Set db = CurrentDb

Set table = tblMaster

For Each tdf In db.TableDefs

If Right(tdf.Name,8) = Year(tblMaster.Date) & Month(tblMaster.Date) & Day(tblMaster.Date)

Then

strSQL = "DROP TABLE " & tdf.Name
db.Execute (strSQL), dbFailOnError

End If

End Sub

Would really appreciate your help! Thank you so much!
MGN


I'ld do it this way on the 'If' just to eliminate any conversion problems:

If Right(tdf.Name,8) = Year(tblMaster.Date) & Month(tblMaster.Date) & Day(tblMaster.Date) then

This way it is 2 strings being compared.

Also have you looked at the input to the If in a MSgBox? How does tblMaster.Date get set - there's nothing in the Sub?
Jan 27 '08 #5
TerryDM
14
Guess I was looking at the wrong 'problem'. Try something like this:

Expand|Select|Wrap|Line Numbers
  1. For Each tdf In db.TableDefs
  2. tdate = CDATE(Right(tdf.Name, 2) & "/" & Mid(tdf.Name, 8, 2) & "/" & Mid(tdf.Name, 4, 4))
  3.  
  4. strSQL = "SELECT * FROM tblMaster WHERE TDate = '" & tdate &"';"
  5. Set rs = db.Execute (strSQL)
  6.  
  7. If not rs.EOF and not rs.BOF then
  8. strSQL = "DROP TABLE " & tdf.Name
  9. db.Execute (strSQL), dbFailOnError
  10. End If
  11.  
Jan 27 '08 #6
Hi,

I've defined rs as DAO.Recordset, then Set rs = db.OpenRecordset(strSQL) as the following shows. I've also included date literals, ie the # characters in front of and behind tdate as below. But I still can't get the code to work? Note: I have kept the field name 'Date' in tblMaster because when I tried the same code below with field name 'TDate' in tblMaster, it didn't work either.

Sub EOFBOF()

Dim strSQL As String
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim tdate As Date
Dim rs As DAO.Recordset

On Error GoTo Proc_Error

Set db = CurrentDb

For Each tdf In db.TableDefs


tdate = CDate(Right(tdf.Name, 2) & "/" & Mid(tdf.Name, 8, 2) & "/" & Mid(tdf.Name, 4, 4))

strSQL = "SELECT * FROM tblMaster WHERE (tblMaster.Date) = #" & tdate & "#;"

Set rs = db.OpenRecordset(strSQL)

If Not rs.EOF And Not rs.BOF Then

strSQL = "DROP TABLE " & tdf.Name
db.Execute (strSQL), dbFailOnError

End If

Next tdf

Proc_Exit:
Exit Sub

Proc_Error:
Resume Proc_Exit

End Sub

Would really appreciate your help on this, thank you so much!
MGN

Guess I was looking at the wrong 'problem'. Try something like this:

Expand|Select|Wrap|Line Numbers
  1. For Each tdf In db.TableDefs
  2. tdate = CDATE(Right(tdf.Name, 2) & "/" & Mid(tdf.Name, 8, 2) & "/" & Mid(tdf.Name, 4, 4))
  3.  
  4. strSQL = "SELECT * FROM tblMaster WHERE TDate = '" & tdate &"';"
  5. Set rs = db.Execute (strSQL)
  6.  
  7. If not rs.EOF and not rs.BOF then
  8. strSQL = "DROP TABLE " & tdf.Name
  9. db.Execute (strSQL), dbFailOnError
  10. End If
  11.  
Jan 28 '08 #7

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

Similar topics

5
by: Andrew | last post by:
Hi All, Have come across something weird and am after some help. Say i run this query where rec_id is a column of table arlhrl, select * from arlhrl where rec_id >= 14260 This returns to...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
5
by: Steve Strik | last post by:
My Problem: I have created a database here at work that is exhibiting some very strange behaviour. Essentially the database is structured in a manner where one table is a master record table...
8
by: Bri | last post by:
Greetings, I am using Eval() in a query with only limited success. If the text within the function contains a reference to a Field I get #ERROR#. I'll try and explain what I'm trying to do and...
12
by: Wadim Grasza | last post by:
I want to store and display (on a form or a report) multiple pictures per record in an access database. The pictures are not stored within the database. They are stored as files and the database...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
9
by: Joshua.Buss | last post by:
I am trying to move a record from one linked table to another within access, but I'm a complete beginner to VBA and don't know exactly where to begin. I have an access file that has the two...
7
by: hjohnson | last post by:
Within the access environment, I have a table that I'd like to -add a column -place the record number of each record into that column The autonumber is not working for me because I am...
6
by: Mark | last post by:
Currently using MS Access 2000 and SQL Server Express. Using the current DAO OpenRecordset code listed below, however I keep getting the error message.... 3254: ODBC --Cannot lock all records ...
10
by: owuraku | last post by:
Hi y'all Can I reference a cell in an access table within a VBA code?? I have 1 x 2 table (1 row and 2 columns). The first column is just an autoID generator. The second column keeps track of dates....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.