Function to open tables

bee55

Registered User.
Local time
Today, 13:19
Joined
Oct 27, 2011
Messages
50
Hi every body,

i have three table in my project , on each form in my project i open three tables either to edit or add
i open these tables an close them too much times on my code
i use the following code
Code:
Dim FoodStuff As DAO.Database
Dim RsFood As DAO.Recordset
   
Set FoodStuff = CurrentDb
Set RsFood = FoodStuff.OpenRecordset("Table1")


is there any way to make a function with name forexample (Open table)
and when i want to open any table i just write open_table("table1")
Great Thanks
 
can you elaborate furthe? cause it seems you can just set an in module variable then use either a case or if statement.
 
So you're concerned about how many times you're opening and closing a DAO Recordset? How many times are you opening and closing it? What is your concern with opening and closing your recordset?

In general, it's common to open and close recordsets many times. It's not a good idea to keep a recordset open if there is no active processing being done on the recordset. If the user changes data on a form there is a good chance that the recordset will throw some kind of curve ball at you.

Please post your code with a more elaborate explanation of WHY.
 
SO my question wasn't clear

i mean, my solution has more than 10 forms,
on each form i open more recordset
so i need a function to be decleared as public like for example:-
Function Connect_Table(Table_Name as string)
connection = d:\database1.mdb

End Function

means i need a common public function so i can open table and close it in just two line
 
i try the following code :-


Code:
Option Compare Database
Public FoodStuff As DAO.Database
Public RsFood As DAO.Recordset

Public Function Open_Table(Table_Name As String)
Set FoodStuff = CurrentDb
Set RsFood = FoodStuff.OpenRecordset(Table_Name)
RsFood.MoveFirst
End Function
Public Function Close_Table(Table_Name As String)
RsFood.Close
End Function

Code:
Option Compare Database
Private Sub Save_Click()
Open_Table "Temp"
MsgBox RsFood!quantity
Close_Table "Temp"
End Sub

the above code was work successfull but if there is another way to the line msgbox RsFood!quantity to be Msgbox Temp!quantity it will be a good method
 

Users who are viewing this thread

Back
Top Bottom