Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2005

Tip: Looking for answers? Try searching our database.

Searching Recordsets in code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Melvis - 17 Mar 2005 17:51 GMT
Hi,

I'm a relatively new database programmer. I am wondering if there is any
kind of tutorial or information on how to properly use recordsets in code to
locate and manipulate data. I am mostly self-taught in Access, and feel like
I missed a pretty important ability here...

As usual, ant and all help is greatly appreciated.
~MATT
sparker - 17 Mar 2005 18:43 GMT
The following code will show you how to use DAO recordsets in access just cut
and paste into a module and study. Hope this helps! - SPARKER

Private Sub subRecordSetDAO()

'Cut an paste this entire example into a module:
'This sample is set up to look at a table named Table1
'with the columns or fields of EmployeeID, FirstName, LastName
'You will need to edit the table and field names to work
'with your own sql query statement. This example is also set up
'to loop through each record that your query pulls one record
'at a time so that you can do something with the data.
'Using this DAO code you will need to add a reference to
'Microsoft DAO 3.6 Object Library
'To add the reference to the Microsoft DAO 3.6 Object Library:
'While you have the module open in the database click on
'Tools then
'References then
'Scroll Down until you find
'Microsoft DAO 3.6 Object Library
'then add it so the following code will work.

Dim daoDbs As DAO.Database                  'Declare Database
Dim daoRec As DAO.Recordset                 'Declare Recordset
Dim intEmployeeID As Integer                'Declare Variables for Fields to
be returned by your query
Dim strFirstName As String
Dim strLastName As String
Dim dteHireDate As Date
Dim strSql As String                        'Declare the Query itself
strSql = "Select * From Table1;"            'Set the query
Set daoDbs = CodeDb                         'Set the Database
Set daoRec = daoDbs.OpenRecordset(strSql)   'Set the Recordset

       If Not (daoRec.BOF And daoRec.EOF) Then 'Check to see if we have any
records to work with
               daoRec.MoveFirst                'Move to the very first record
         Do While Not daoRec.EOF               'Start Loop and continue
until we run out of records

           intEmployeeID = daoRec("EmployeeID").Value  'Set values from our
query to our variables
           strFirstName = daoRec("FirstName").Value
           strLastName = daoRec("LastName").Value
           dteHireDate = daoRec("HireDate").Value
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'In here you can do whatever you want to do with the data such as alter,
append, delete, transfer
'Lets say place employees last name Smith Hired recently
'into a temp table named Table2 for further manipulation etc...
'Yes I know this sounds rediculous but it is just a hypothetical
'example to show the use of DAO recordsets

Dim strSqlAppend As String          'Declare and set our SQL Append Query
strSqlAppend = "INSERT INTO Table2 ( EmployeeID, [First Name], [Last Name] )
" & _
"SELECT " & intEmployeeID & ", " & strFirstName & ", " & strLastName & " ;"
'If Last name is Smith and employee was hired in the past 30 days then
If strLastName = "Smith" And dteHireDate > Date - 30 Then
   CodeDb.Execute strSqlAppend         'Append Employee
End If

'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
           daoRec.MoveNext                     'Move to the next record
         Loop                                  'Loop back to the start of
the loop
       Else                                    'Else the check said there
is no data to work with
           MsgBox "This Query Produced 0 Records To Work With.",
vbInformation, "No Records:"
       End If
End Sub

> Hi,
>
[quoted text clipped - 5 lines]
> As usual, ant and all help is greatly appreciated.
> ~MATT
Melvis - 17 Mar 2005 19:37 GMT
How would this be effected if the database id to be upsized to a Microsoft
SQL Backend?

> The following code will show you how to use DAO recordsets in access just cut
> and paste into a module and study. Hope this helps! - SPARKER
[quoted text clipped - 78 lines]
> > As usual, ant and all help is greatly appreciated.
> > ~MATT
sparker - 17 Mar 2005 20:18 GMT
There will be no problem running this code on linked SQL Server tables in an
Access database.
When using a SQL Server backend you link the tables into your Access
database and they function almost identical to Access tables.

Note: When using SQL Server 2000 for a back end try to do as much of your
code writing and data manipulation using the SQL Server Native SQL called T -
SQL using stored procedures, views, triggers, and DTS. That is much faster...

Let me know if I can be of any more assistance. - SPARKER

> How would this be effected if the database id to be upsized to a Microsoft
> SQL Backend?
[quoted text clipped - 81 lines]
> > > As usual, ant and all help is greatly appreciated.
> > > ~MATT
Marshall Barton - 17 Mar 2005 18:55 GMT
>I'm a relatively new database programmer. I am wondering if there is any
>kind of tutorial or information on how to properly use recordsets in code to
>locate and manipulate data. I am mostly self-taught in Access, and feel like
>I missed a pretty important ability here...

I don't know of a tutorial, which is not to say they don't
exist.  I suggest that you start by looking up Recordset in
Help and follow the links to all its Properties and Methods.
Be sure to distinguish the difference between DAO (probably
the one you want) and ADO recordset objects.

Signature

Marsh
MVP [MS Access]

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.