Populate Time Range In MS-Access - ms-access

I am making a scheduling tool, which runs off of five-minute increments tied to the IDs of timeslots on another table. These timeslots have a start time, and stop time which are separate fields in the same record, and can be entered using a form, and I would like to create a range of five-minute increments as records between these two times.
The ultimate goal is to use these timeslots to make an outlook-style gui to show when there are timeslots.
I have been struggling to find a quick way to fill them in Access, and my attempts at coding are so pathetic I'm too embarrassed to show them.
Any advice would be greatly appreciated,
Thank you!

Sounds like you want to create a batch of five-minute increment records associated with a particular date. More than one way to accomplish. Simplest may be to have a table of time records. Each record would have a time value: 10:00, 10:05, etc. Then on UNBOUND form user selects time range start and end from comboboxes and enters a date in a textbox. An SQL action statement in VBA could be:
CurrentDb.Execute "INSERT INTO tblSchedules(SchedDate, SchedTime) " & _
"SELECT #" & Me.tbxDate & "#, Mins FROM tblTimes " & _
"WHERE Mins BETWEEN '" & Me.cbxStart & "' AND '" & Me.cbxEnd & "'"
Without this table of time records, VBA would use a looping structure to save one record at a time to destination table.
Dim strT As String, db As DAO.Database
strT = Me.cbxStart
Set db = CurrentDb
Do While strT <= Me.cbxEnd
db.Execute "INSERT INTO tblSchedules(SchedDate, SchedTime) VALUES(#" & Me.tbxDate & "#, '" & strT & "')"
strT = Format(DateAdd("n", 5, CDate(strT)), "hh:nn")
Loop
In these examples, time value is text, not an actual date/time type. This means a structure of "hh:nn" in 24-hour time: "01:00" ... "24:00". If you prefer to save as number or as a date/time type, modify tables and code as appropriate. Keep in mind that midnight for date/time type is stored without a time component and Format() function displays as 12:00:00 AM or 00:00:00. Example: Format(#1/1/2022#, "mm/dd/yyyy hh:nn:ss AM/PM") displays as 01/01/2022 12:00:00 AM. Ranges crossing midnight causes complication.

Related

MS Access VBA Writing to a table from a form

I have a form (Purchase Orders) and a sub-form in it
(Purchase Order Details). The Main form contains Number of the
PO (text box), Supplier (combo-box), Employee (combo-box),
Status (combo-box) which contains 2 records (New and Done) and a date box (when the PO was created). The Sub-form (datasheet) contains Product (combo-box), Quantity and a Price field.
What I want to do is to add a button on the main form which will do
next.
When the button is pressed a VBA code should be executed and do next.
Take data from the Main form (Number of the PO, Status and the date)
and the Sub-form (Product, Quantity and Price) and put all that into
a table (StockMovements).
I managed to do that with the next code:
Private Sub cmdOrder_Click()
Dim strSQL As String
strSQL = "INSERT INTO StockMovement (ID_Product, Status, Quantity, ID_PurchaseOrder) VALUES (" & _
Me.frmPurchaseOrderDetails_Subform.Form!comboboxProduct & ", '" & _
Me!txtStatus & "', " & _
Me.frmPurchaseOrderDetails_Subform.Form!txtQuantity & ", " & _
Me!txtID_PurchaseOrder & ");"
DoCmd.RunSQL strSQL
Me.Requery
End Sub
However, there are 2 problems:
As you can see I didnt add the date field because I get an error,
cant remember which one it was exactly but I think 2075;
The code works without the date, but only adds one Product to the
table, the first one. And in a Purchase Order there are usually more
than one products.
Because Im totally new in VBA, I would kindly ask you to treat me like a newbie and explain more detailed, if possible.
(Fixed the code, forgot to change the language. I mean I pasted the wrong one, now its the right one but still not working of course :))
Thanks!
1) If you only have a problem with one specific field, I would check whats special with that one. Probably the input is formatted as a string and the field as Date/Time. In that case try to use the CDate()-Function. I could imagine, that a .Value at the end could solve the problem, too.
Dim datDate as Date
datDate = CDate(Me!txtDate.Value)
2) That your code inserts only one row is absoluterly correct. Remember that e.g. Me!txtStatus.Value is a textbox that contains only one single piece of data. The rows of data are saved in a table and depending on the row you have selected with a main-form (= one row), the corresponding value is shown in the textbox.
INSERT INTO table (field1,field2) VALUES (value1,value2)
An INSERT INTO inserts one row every time it's executed. So the SQL in the code you have mentioned needs to be repeated. You could do so using loops.
Dim strSQL As String
strSQL = ""
For Each Item In Group
strSQL = strSQL & "INSERT INTO table (field1,field2) VALUES (value1,value2)"
Next
In my opinion, copying data that way is absolutely annoying with VBA. You need to create recordsets, modify and merge them. I would try to solve as much as possible with Access Non-VBA-Solutions.
My question to you: Did you think about linking the form (and sub-form) directly to the table(s)?
The date can be inserted this way:
"INSERT INTO StockMovement ([DateField]) " & _
"VALUES (#" & Format(Me!DateField.Value, "yyyy\/mm\/dd") & "#)"
That said, your code will insert one record only. To insert multiple records you need a select query or - much faster - use DAO to open the target table as a recordset and then, as source, loop the RecordsetClone of your subform and copy the records to the target table one by one.

MS Access 2013 copy a specific number of fields and paste into a new record

I have found similar answers to this question, even on this site, however, the syntax has not worked for my database and I'm not sure what needs to be done. This data base is used to house audits for staff performance and accuracy. I am now in the midst of creating the forms and getting them to flow properly for the user.
When conducting an audit, the user will need to enter six specific fields into the first form. Those forms are Audit, Month, Year, Username, Location, Reviewer, and Date. The user will need to complete multiple audits, however, these six fields will always be the same.
I would like to copy these fields in the first form and carry them into the second form so the user does not have to repeat the information. Here is my current code (set to run on the click of a command button on the bottom of screen 1):
Dim strSQL As String
strSQL = "INSERT INTO [tblTripMem] (Audit, Month, Year, Username, Location, Reviewer, Date)"
strSQL = strSQL & " Values (" & Me.cboFP1Audit & "," & Me.Month & "," & Me.Year & "," & Me.Username & "," & Me.Location & "," & Me.Reviewer & "," & Me.Date & ") FROM [FPScreen1]"
strSQL = strSQL & "WHERE (currentrecord = " & Me.CurrentRecord & ")"
DoCmd.RunSQL (strSQL)
Each time I run this I receive the following error: "Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
I am new to Access and am unsure of what this means or how to fix it. All I know is that I'm not finding a solution. Can anyone help? I'd greatly appreciate it.
Here's a mock-up Access file illustrating a way to do what you're doing without using SQL:
With Form 1 open...
...complete the various fields:
Click the Copy to Form 2 button and this will open Form 2 and populate its fields with the data from Form 1:
Here's the VBA code on the Copy to Form 2 button's OnClick event:
Private Sub cmdCopyToFrm2_Click()
Dim frm As Form
DoCmd.OpenForm "Form2"
Set frm = Forms!Form2
With frm
!AuditRef = Me.cboFP1Audit
!AuditMonth = Me.txtAuditMonth
!AuditYear = Me.txtAuditYear
!AuditUsername = Me.txtAuditUsername
!Location = Me.txtLocation
!Reviewer = Me.txtReviewer
!AuditDate = Me.txtAuditDate
End With
End Sub
Note that when Form 2 opens, the textbox that the cursor defaults to might not seem to show any data; if you move away from that textbox it should magically show (don't know why it does this, but there you go).
INSERT INTO table (...) VALUES (...) cannot have a FROM or WHERE clause. You insert a single record with fixed values, not data from another table.
But once you delete these clauses, you will have other errors, because you need to format your string and date values correctly to get the INSERT query to work.
And then you will still be prone to SQL injection errors. It is safer to use Recordset.AddNew and .Update to add records.
See e.g. here: https://stackoverflow.com/a/34969410/3820271
(but without the loop)

Why are duplicate records being generated in the table?

I am using MS Access 2010. I have a form that captures a couple data items and then writes the record to a table using a function that I defined. The function is called in the click event of the Write Record command-button in the form.
Form:
Code:
> Private Sub writerecord_Click()
Rem MsgBox ("in write record click event")
Dim sqlString As String
Rem 1 - write to the kit_items table
sqlString = "INSERT INTO kit_items (item_vendorid, item_name, item_description, item_cost) VALUES (" & Me!vendorlist & ", '" & Me!item_name & "', '" & Me!item_description & "', '" & Me!item_cost & "');"
DoCmd.RunSQL sqlString
MsgBox ("done writing to table kit_items")
End Sub
records saved in table looks like this
**What is generating the duplicate record ?
If your form is bound to the table that you are writing to, the button will add a second record.
If you would like to work with data this way, you would need to make sure that the form is unbound.
Beware however that if you don't take steps to 'clear' out the data once the user clicks the button to write the record, every time they click the button you will get a new record.
Is there some reason why you need to do the inserting manually (as opposed to letting the bound form handle the inserts/edits)?

How to take out ageing of two rows in access right from vb6.0 code

I have designed database in access and program is designed in vb6.0 but problem is I have two rows in table
Expiry date and today's date
I want ageing days to change everyday automatically by writting code in main window of program to show it on mshflexgrid. Like I have written a code to get change the today's date to be updated automatically everyday as I will not know no. of rows in table in same way I need code for ageing.
Code for today's date is:
Dim smt As String
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
If rs.State = 1 Then rs.Close
smt = "update bgentry set today ='" & Format(Date, "dd/mm/yyyy") & "'"
rs.Open smt, cn, adOpenStatic
bgentry.today = bgentry is name of table and today is name of field in table to show a today's date.
Sample data is when main window of program will get focus:
In the existing table called bgentry today and age all the datas of this two column should be modified automatically by code. For example:
Expiring Date Today's Date Age
19/02/15 20/02/15 1
Expiring date will be added by user every day but today's date and age should be changed everyday in the database so that this report can be shown in mshflexgrid.

how to get the data from the mysql database where date is between the value of 2 datetimepicker

im using vb.net 2008 and mysql in xampp. i have two datetimepicker from form7. when the "display report button" is click then the summary form will show.it contains the datagridview that contains the informations. im trying to get the data from the database where the date_drop is between the value of two datetimepicker. Hope you can help me. Thanks in Advance :)
--here's my code for that but it doesnt give any data :(
ct = "Select j_id as 'Job Code',
cus_name as 'Customer Name',
date_drop as 'Date Drop'
from jobs
where date_drop between '" & DateTimePicker2.Value & "'
and '" & DateTimePicker1.Value & "' "
You mention datepicker1 is the start time and datepicker2 the end time.
So, even if the values from your datepickers are formatted correctly for presentation to MySQL, your query ends up saying something like
BETWEEN '2014-09-28' AND '2014-09-26'
There are no dates in that range; you need to provide the start date first in the BETWEEN clause, like so
BETWEEN '2014-09-26' AND '2014-09-28'
See how the earlier date comes first?
In actual fact, you'd be wise to use this formulation instead
where date_drop >= '" & DateTimePicker1.Value & "'
and date_drop < '" & DateTimePicker2.Value & "' + INTERVAL 1 DAY "
That gets the two ends of the range in the right order. It also works in cases where date_drop might not be precisely at midnight on the end date.