Get date and time from MS Access database using visual basic - date

Good day! I would like to ask what are the syntax in getting the date and time from the MS Access database? the name of the database is "DatabaseTest", the name of the table is "Test" and the name of the field in the table is "schedDateTime". Any help will be greatly appreciated. Thanks!

Assuming that you interested in the time and date that a particular record was created, the easiest thing you can do is create a field named something like "EntryDate" set the date type to "Date/Time". Go under the field properties and select Format: " General Date" then type =now() into the Default Value.

I think your question could use a little clarification. Do you need help parsing a Date/time field, or do you need help figuring out what sql-type query to use?
Private Sub Form_Load()
Dim db As Database
Dim recSet As DAO.Recordset
Dim searchID As Integer
' Set searchID to the ID you are looking for
searchID = 1
' Set up the database
Set db = CurrentDb
' Get the value from the database
Set recSet = db.OpenRecordset("SELECT [schedDateTime] FROM [Test] WHERE ([ID] =" & searchID & ")")
' Where textBox is the name of a text box in your form
[textBox] = recSet(0)
End Sub
In the above, I have shown how to retrieve a value from a database and store it in a form. If this is similar to what you want, I am sure you can adjust the code to your needs.

Related

Is there a way to delete main form data if data in subform is empty?

I have a form in MS Access that has a subform.
My problem is that if the user starts filling the main form with date, customer, IDOrderNumber etc. and suddenly wants to leave the form without entering data in the subform, when he tries to find that form with the same IDOrderNumber, he can't. But when he enters the same IDOrderNumber he can't because it's the primary key and can't allow duplicate values.
What should I do?
I tried to add a search field for IDOrderNumber but it doesn't work - it shows empty Master and Child form/subform. Also I have an Order list Form and I can't access a form that has no subform data entered..
I need a solution because it is a big problem for my customer/user of database.
Thanks all in advance!
The way it sounds to me, is that you want the record in the main form to be deleted if no data is entered in the subform. Ill assume the data in the subform is bound to a different table, and used for multiple records tied to IDOrderNumber
I would suggestion using the below vba code. I'm assuming some of your database's structure, so correct me as needed
This code will go in your MainForms Form_Close() Event
Private Sub Form_Close()
Dim recordexists as Integer
Dim db as Database
Dim rs as Recordset
Dim strSQL as String
strSQL = "SELECT * FROM YourSecondaryTable T2 WHERE T2.IDOrderNumber =" & Me.IDOrderNumberField.Value
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
'This recordset is checking to see if a value in the secondary table exists with the OrderNumber that exists in the main table.
With rs
If Not .BOF And Not .EOF Then
.MoveFirst
While (Not .EOF)
'If the code gets to this point, then a record exists in your subform.
'So we set the value to 1
recordexists = 1
Wend
End If
.Close
End With
'If the above recordset didnt find anything, the the recordexists value would never be set.
'Therefore you want to delete the parent record. So we delete it
If recordexists = 0 Then
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE TOP 1 FROM YourMainTable T1 WHERE T1.IDOrderNumber = " & Me.IDOrderNumberField.Value
DoCmd.SetWarnings True
End IF
Set rs = Nothing
Set db = Nothing
End Sub

Using a Combo Box as a data entry on Access Form

I was wondering if you can use a Combo Box on a form in access as a data entry for a query.
For instance, I have a form based on a query and I want to use a Combo Box to edit a field that is blank on the query end based on the values that are in the Combo box, similar to a drop-down box in a query or table.
I created a Column for the data values to be entered and the values will go into a given based on the record page the combo box selection is made on.
I can add additional information if needed.
I'm a bit confused here, but it sounds like you want to use a pass a parameter into a query. Is that right? Something like this, right.
If this is going in the direction you want, read through the links below, and download the sample files at the bottom of the URL.
http://www.fontstuff.com/access/acctut17a.htm
http://www.fontstuff.com/access/acctut18.htm
You need to create a base query that will contain all of your data, Then for each combo box you will need to recreate and run the query. I usually program this on the after update event. I also create and store the query so that when a user returns to the form it returns to the same data as before. Just be warned that this could cause errors when the query is executing and updating subforms.
Example on your Form
'Assumes Combo1 has the data to filter and Table1 is source and Query1 exists
Private Sub Combo1_AfterUpdate()
Dim Obj_QueryDef As Object
Dim Temp_QueryName As Variant
'Save Query Name for refreshing Form Data (Query will be overwritten!)
Temp_QueryName = Me.Form.RecordSource
'dereference the query definition object that already exists
Set Obj_QueryDef = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs(Temp_QueryName)
'For Number Key
Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE [Field1] = " & Combo1 & ";"
'Reset the Record Query then repaint the form
Me.Form.RecordSource = Temp_QueryName
Me.Form.Repaint
End Sub
'To add Add More Combo Boxes, After update Regenerate SQL adding & " AND Field2 = " Combo2 & ";"
'Or better yet Create a Function that handles the SQL statement
'You can use the same idea to limit the items that appear in the dropdown selection if you update
' the record source for the combobox
'reference for different data types:
'String Data 'String Value' use chr(39) = '
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(39) & Combo1 & Chr(39) & ";"
'For Date Use #Date Time# Use chr(35) = #
'Obj_QueryDef.SQL = "SELECT * FROM Table1 WHERE Field1 = " & Chr(35) & Combo1 & Chr(35) & ";"

MS Access Forms: Control Default Values and DLookup

I've tried searching on here for my answer but have had no luck. I've tried many things to get this to work but haven't been successful so I thought I would post my question. I may repeat myself several times but I'm trying to be as specific/detailed as I can.
I have a form which has an append query to transfer all fields on the form into a table. On this form, I have an invisible control (textbox) which pulls the users Windows login ID and uses that as its default value. I have another field (textbox) for the user's full name.
I have ran a query to pull employee data from my tables to have the employee ID (same as their Window's login ID, first name, and last name. I have also ran an expression to concatenate the two name fields into one to show their full name.
What I've been trying to do is have it so that when someone opens up the form, their full name is already pre-loaded. As I stated above, this is pre-loaded based on a comparison of their Window's login ID with their employee ID that we have on record as they're the same. It's essentially Excel's version of vlookup() that I'm trying to do. I've tried dlookup() but was unsuccessful.
Does anyone know how to make the default value of the employee name control on the form to be determined by employee ID control which has its default value based on the user's Window login ID?
Example: Let's say that I have an employee called Fred Flintstone. In the database there's a table with fields for his first name and last name. There's also a field for his employee ID which is A111111.
When opening up the form, there's an invisible textbox control which has its default value derived from the Windows login ID that the employee used to sign onto the computer with. This Windows login ID happens to be A111111 which is the same as the employee's employee ID.
A query was created that has fields for the employee ID, First Name, Last Name, and an expression to concatenate the two name fields. So the first row would be: | A111111 | Fred | Flintstone | Fred Flintstone |. I don't know how to make tables on here yet so bear with me.
Going back to the form, there's a field for Employee Name. How would I get it to show Fred Flintstone as the default value upon opening the form?
Note: The employee name field will eventually be locked so that it cannot be changed by the employee. I don't know if that would have any impact but I thought that I would mention it.
In the onload event of your form (really rough code, will require some tweaking..):
dim db as dao.database
Dim rs as dao.recordset
Dim username as string
dim SQL as string
username = Environ("USERNAME") 'There are better ways to get username, but this is just a quick example.
SQL = "SELECT * FROM Your_Query WHERE username = '" & username & "'"
set db = currentdb
set rs = db.openrecordset(SQL,dbopensnapshot)
if rs.recordcount > 0
rs.movefirst
me.UserNameField = rs!username
else
msg "User Not Found"
End If
set rs = nothing
set db = nothing

How to add a multiple values field to an MS Access form

I have a database which records and tracks the training records for each employee within our group of companies.
One of the forms on the database is 'add training record' and currently this is done per person ID, adding the course id, date, certificate, etc. What I would love to do is be able to add multiple person IDs, as a number of people can attend the same course/ date, so I have to add the course details in many times, which is very time consuming and annoying.
You're going to need extensive VBA behind the scenes to do this, as well as a couple of controls to make your life easier (multi-select combo or list boxes). You could do it in a text box if you want, using the semi-colon between User IDs.
Since the text box is easier, let's look at that. What you would do is have something like this behind the Submit button:
Dim db as Database
Dim rec as Recordset
Dim str as String
Set db = CurrentDB
Set rec = db.OpenRecordSet("SELECT * FROM tblMyTable")
'Throw the User IDs into an array
Dim var As Variant
Dim i As Long
str = me.txtUserIDs
var = Split(str, ";")
'For every User ID, add a new record to the database
For i = 0 To UBound(var)
rec.AddNew
rec("UserID") = var(i)
rec("CourseID") = Me.txtCourseID
rec("CourseDate") = Me.txtCourseDate
'ETC... adding fields as needed
rec.Update
Next i
This is completely off-the-cuff and (of course) requires you to change the appropriate variable names, but it should give you a solid outline for how to solve the problem.

Trying to design form to query and edit records Access

I'm trying to make an Access database where we can scan in a list of bar-codes, batch edit information for them, then save all the records. Later, they will need to be scanned again to batch edit more information such as shipping and order numbers.
Does anyone have suggestions on how to best design this? To start with, I've (1) made separate tables for "orders" and "parts" and (2) created a form of text boxes and a button attempting to apply this info to a part subform where records are created via the bar-code scanner. I'm a rather new Access designer. Thanks!
The questions are:
I'm not sure how to query these records via the bar code (Part_ID field) to apply order information to them later.
I can't currently update all new part records at once through my form.
Here's a little guide on how to do it. Let me know if you have any questions.
Dim db As DAO.Database
Dim Rs As DAO.Recordset
'Here is an example of a vba qarry in access. tblOrdersWhere is obviously your table.
'orderNum is a column name in your table pref a primary key
'txtOrderNum.Value is the value of a text box
'If you want just a simple select * from table you can put after db.OpenRecordset("tblOrders")
set rs = db.OpenRecordset("Select * from tblOrders Where orderNum like '*" & txtOrderNum.Value & "*';", dbOpenDynaset)
'This is how you update records in Access
'If you want to update it's Rs.Edit instead of Rs.AddNew
Rs.AddNew
Rs("OrderItem").Value = txtGroupName.Value
Rs("OrderTime").Value = txtGroupNum.Value
rs.Update