Working in the background

B

Bernie Richard

I 'm looking for help on code that I can use to manipulate data in a table in
the background without it being visible to the user. I've used Paradox where
I would use a tCursor tehnique where I could search for records and update
fields as well as add records all in the background. What code would I use
to do this in Access. Just need the basics to get me started.

Thanks
 
J

John W. Vinson

I 'm looking for help on code that I can use to manipulate data in a table in
the background without it being visible to the user. I've used Paradox where
I would use a tCursor tehnique where I could search for records and update
fields as well as add records all in the background. What code would I use
to do this in Access. Just need the basics to get me started.

Thanks

It's not at all clear what manipulations you want to do, but running an Update
query will update existing records, and an Append query will add new ones. The
preferred way to do so without bothering the user is to use the Execute method
of the Application object:

Dim db As DAO.Database
Dim strSQL As String
On Error GoTo Proc_Error
Set db = CurrentDb
strSQL = "UPDATE mytable SET thisfield = " & lngNumber & ", thatfield = '" _
& strString & "' WHERE someconditions;"
Application.Execute strSQL, dbFailOnError
Proc_Exit:
Exit Sub
Proc_Error:
<appropriate error handling messages>
Resume Proc_Exit
 
T

Tom van Stiphout

On Sat, 2 Jan 2010 09:14:01 -0800, Bernie Richard

It depends on the specifics. Access is not multithreaded to the normal
VBA programmer so actually running things concurrently is difficult.
I'm sure PDox had the same limitation.
Updating data is usually done with an Update query. The query can be
run in a variety of ways; DoCmd.RunSQL is a quick and dirty method.
The Execute method of the Querydef object gives you much more control.
You could potentially run such query in a Timer; that would give it
the appearance of running concurrently with other code. But much more
likely you would run it when the conditions warrant it, for example
when the user inputs certain values, or moves to the next record, etc.
There are event procedures (at the Form level and the Control level)
for each of those user actions.

-Tom.
Microsoft Access MVP
 
D

David C. Holley

What exactly do you need to do and is there a specific reason you don't want
it visible to the user?
 
B

Bernie Richard

I want to enter data one record at a time. Specifically I'm designing a
Data Entry Form that collects information such ad Employee Name, a Datetime
stamp when "Checking In, a DateTime Stamp when Checking out and several
fields that are answered Yes or No. I developed this in Excel but thats not
the right environments for the amount of data I'm collecting so I'm trying my
hand at Access. The Form has a dropdown list for Employee Name and two
buttons, one labled Check iN and one Labled Check out. The them employee sees
the form it will display a dropdown list for him to select his name. If he
is "Checking In the code associated with the button will search the Table to
see if the employee has already checked in and mistakenly selected the wrong
button. This is where I need to search the Table for the Employee Name and
the Field that has the Timestamp for Check in. If I find that the employee
has already 'Punched in today the I generate a message and return control to
the user. If the search doesn't find the inceddence of and employee who
punced in today then I add a record, populate the Employee Field and the
Check in Field. A Similar sequence occurs when the employee Checks out.
Again I need to find the record that contains the employee name and a check
in date of today with no check out data. Etc, etc.
The reason for keeping the data hidden is that I don't want employee seeing
this information by navigating through records or being able to manipulate
any data.
This is a relatively simple task and has only one point of data entry at a
sinbgle terminal. I also created this application in PAradox but They would
rather us Access.
 
D

David C. Holley

The original post now makes sense, especially that you mention that you've
been working with Excel.

You're essentially looking at validation which is confirming the state of a
value or a record before or after an update.

Within Access, there are events that fire for the Form object as well as the
control objects ( a list box, a drop down, a text field, etc.) All of the
form & control objects have a _BeforeUpdate and a _AfterUpdate event which
fires (obviously) before changes are committed to the table and afterward.
The _BeforeUpdate event allows you to intercept the event and if needbe
cancel the action. However, given your scenario you can setup the drop down
list to show either 'ALL EMPLOYEES', 'EMPLOYEES NOT CHECKED IN', or
'EMPLOYEES NOT CHECKED OUT'. The .RecordSet property of the drop down list
is a SQL statement, table name or query that pulls the relevant data and
populates the list. Sample SQL statements are below...

ALL Employees
SELECT FirstName, LastName FROM Employees ORDER BY LastName, FirstName

Employees Not Checked In
SELECT FirstName, LastName FROM Employees WHERE IsNull(CheckIn) ORDER BY
LastName, FirstName

Employees Not Checked Out
SELECT FirstName, LastName FROM Employees WHERE IsNull(CheckOut) ORDER BY
LastName, FirstName

If the check in/check out information is in a separate table, obviously the
SQL statements would have to be modified to include that table. (Post back
if you need help on that).

Obviously, restricting the options in the drop down list somewhat eliminates
the need to validate the selection since you've already effectively done
that. However, if you DO need to validate the selection, you'll use code
similar to this...

Private Sub cboDispatchStatus_BeforeUpdate(Cancel As Integer)

On Error GoTo Err_cboDispatchStatus_BeforeUpdate

Dim strMsgText As String

strMsgText = ""

If Me.txtLoadStatus <> "Ready for Dispatch" Then
strMsgText = "This trailer cannot be dispatched until the load
status is updated to 'Ready for Dispatch.'"
MsgBox strMsgText, vbInformation + vbOKOnly
Cancel = True
Exit Sub
End If

If IsNull(Me.cboDispatchLocation) = True Then
strMsgText = strMsgText & "-The trailer dispatch location must be
selected first, before the status can be updated to 'Dispatched'." & Chr(13)
Me.Undo
MsgBox strMsgText, vbInformation + vbOKOnly
Cancel = True
End If

Exit_cboDispatchStatus_BeforeUpdate:
Exit Sub

Err_cboDispatchStatus_BeforeUpdate:
MsgBox getDefaultErrorMessage(Me.Name, "cboDispatchStatus_BeforeUpdate",
Err.Number), vbCritical
Resume Exit_cboDispatchStatus_BeforeUpdate

End Sub

In this example, there are two conditions that if either or both are true
will CANCEL the update by simply setting CANCEL = TRUE.

In your scenario, you would most likely use a DLOOKUP() to pull the
information from the relevant table and then check it as in...

EmployeeCheckIn = DLookup("CheckIn", "EmployeeStatus", "Id = '" &
cboEmployeeDropDownList & "' AND Date=Date())
EmployeeCheckOut = DLookup("CheckOut", "EmployeeStatus", "Id = '" &
cboEmployeeDropDownList & "' AND Date=Date() AND IsNull("CheckIn") = False")

Note: When you declare the variables that will hold the result of the
DLOOKUPS declare them as variants as 'Null' is a valid result from a
Dlookup()

Dim EmployeeCheckIn as Variant
Dim Employee CheckOut as Variant

If the employee has NOT checked in, the DLookup() will return NULL. Likewise
an employee that has not checked out (but has checked in) will be null as
well. As the DLookup()'s are written, they are only looking at today's date
meaning that there could be an employee record from an earlier date where
the employee checked in, but has not checked out. While this is entirely
possible for employees who work graveyard 11:00 PM - 6:00 PM, it does allow
for situations where an employee from three days ago left without checking
out. You'll need to decide how to handle *both* scenarios.

Once you have the DLookup's working then its just a matter of tweaking the
code as in

If Not IsNull(EmployeeCheckIn) then
MsgBox ("Employee has already checked in. Check in date & time:"
& EmployeeCheckIn
Cancel = True
end if

The *NOT* is neccessary because if the DLookup for EmployeeCheckIn returns a
value, the variable will NOT be Null (Empty). The same scenario applies to
the EmployeeCheckOut.

You will have to a means by which you indicate that the user is in
'Check-In' mode versus 'Check-Out' mode as the logic for validating the
record will be different.
 
D

David C. Holley

I'm just a victim of modern day technology wherein you type the wrong word
while updating your Facebook status, read it and completely miss the
mistake.

But yes... .RecordSource
 
B

Bernie Richard

Just getting back to this. Thanks to all of you who replied and actually read
this far back. I think I've got enough tot get it done. If not, I'll be
back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top