Date function in access vba outputting time - forms

I am trying to update a date field in a table to the current date when a button is clicked on a form and that given field is empty. However, when the field is updated, the output is the time 12:00:05, not a date at all. When year(date) is used instead, 7/7/1905 is the output. I am not sure why these values are my outputs. Here is my code:
Dim ctl As Control
Set ctl = [Forms]![frm1]![subfrm1].[Form]![CloseDate]
If IsNull(ctl) Then
DoCmd.SetWarnings False
DoCmd.RunSQL "Update tbl1 SET [CloseItem] = ""YES"" WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [Item#] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
DoCmd.RunSQL "Update tbl1 SET [CloseDate] = " & Date & " WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [ItemID] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
' Output is 12:00:05
' OR DoCmd.RunSQL "Update tbl1 SET [CloseDate] = " & Year(Date) & " WHERE [ID] = " & [Forms]![frm1]![ID].Value & " AND [ItemID] = " & [Forms]![frm1]![subfrm1].[Form]![ItemID].Value
DoCmd.RefreshRecord
' Output is 7/7/1905
End If
How can I get it to output the correct date?

Examine the string your code builds for this segment of the UPDATE statement. (This is an example copied from the Access Immediate window. You can go there with Ctrl+g)
? "Update tbl1 SET [CloseDate] = " & Date & " WHERE [ID] = "
Update tbl1 SET [CloseDate] = 2/16/2015 WHERE [ID] =
The db engine does not see 2/16/2015 as a Date/Time value. Instead, it treats that as 2 divided by 16 divided by 2015. And the resulting number, when expressed as a Date/Time value, gives you this ...
? Format(2/16/2015, "yyyy-m-d hh:nn:ss")
1899-12-30 00:00:05
You can signal the db engine that 2/16/2015 is a Date/Time value by enclosing it in # delimiters like this: #2/16/2015#
However, since the db engine understands the Date() function, you can use that function name directly in your UPDATE statement and not bother about concatenating in a value with proper delimiters.
Dim strUpdate As String
strUpdate = "Update tbl1 SET [CloseDate] = Date() WHERE [ID] = " & _
[Forms]![frm1]![ID].Value & " AND [ItemID] = " & _
[Forms]![frm1]![subfrm1].[Form]![ItemID].Value
Debug.Print strUpdate
CurrentDb.Execute strUpdate, dbFailOnError

Related

MS Access Problem with DateAdd in VBA Insert statement with inconsistent date format

I'm having problems as to how a date is being stored in my DB.
I need to create a temporary TableC into which will be inserted X records for each record on TableA. Where X is the number of weeks. The resulting table will be used in a UNION statement with several other queries.
The problem lies in how DateAdd creates the date for each new record. Dates are originally stored in dd/mm/yyyy format. But the resulting DateAdd("ww", i, rst![Date]) will sometimes be stored in mm/dd/yyyy format creating havoc in the 30K resulting rows.
Wrote a VB Sub to create the table and bellow is a sample as to how it is actually stored vs what was expected.
Sub AddItems()
Dim db as DAO.Database
Dim rst as DAO.Recordset
Dim Sql1, Sql2 as String
Sql1 = "SELECT [a].*, [b].[Date], [b].[Weeks]. [b].[Rate] FROM TableA as [a] LEFT JOIN TableB as [b] WHERE [a].[GroupId] = [b].[Id] ORDER BY [b].[Date], [a].[Id];"
Set db = CurrentDb
Set rst = db.OpenRecordset(Sql1)
rst.movefirst
While NOT rst.EOF
If rst![Weeks] > 0 Then
For i = 1 to rst![Weeks]
Sql2 = "INSERT INTO TableC ([ID], [CUSTOMER], [DATE], [AMOUNT]) VALUES ("
Sql2 = Sql2 & rst![ID] & ", " & rst![CUSTOMER]
Sql2 = Sql2 & "#" & Format(DateAdd("ww", (i - 1), Format(rst![Date], "mm/dd/yyyy")), "mm/dd/yyyy") & "#"
Sql2 = Sql2 & ", " & rst![AMOUNT]
Sql2 = Sql2 & ")"
Debug.Print Format(DateAdd("ww", (i - 1), Format(rst![Date], "mm/dd/yyyy")), "mm/dd/yyyy")
db.Execute(Sql2)
Next i
End If
rst.movenext
Wend
End Sub
RESULTING TABLE SAMPLE
+-------------+------------+------------+
| Week | On Table | On Debug | Expected Data Inserted
+-------------+------------+------------+
| 1 | 12/02/2019 | 02/12/2019 | Should be 2 / Dec / 2019
| | as 12/Feb | Correct |
+-------------+------------+------------+
| 2 | 12/09/2019 | 09/12/2019 | Should be 9 / Dec / 2019
| | as 12/Sept | Correct |
+-------------+------------+------------+
| 3 | 16/12/2019 | 16/12/2019 | Should be 16 / Dec / 2019
| | Correct | Correct |
+-------------+------------+------------+
The results printed on the Immediate Window are correct, yet on the actual table the information is incorrect. My sample data starts on 02/12/2019 (2 / December / 2019 as confirmed with the date selector on table view in TableA)
The debug window shows the correct information to be stored, yet on the table is incorrect eventhough the field [DATE] in TableC is formated with "Short Date" and with an IsDate validation rule.
Without the two Format statements the results where extremely skweed from what was expected. Yet the resulting DATE field isn't consistent.
IS THERE A WAY TO CONSISTENTLY GENERATE DATES AND STORE THEM ACCORDING TO THE SYSTEM'S SETTINGS ?
First some remarks:
Variable i wasn't declared at all, you should use Option Explicit in the header of the module to enable the VBE to warn you about.
Date is a reserved word, so I renamed it to YourDate.
Variable sql1 was implicitly defined as type variant.
Your definition of the SQL for variable sql1 wasn't syntactically correct. I corrected it and hope it is what you meant.
I expect that your table field YourDate is of type date and not string. If it is string then you need to convert it to date first in the code, or much better in the table.
Always use the property Value explicitly if you implicitly want to use it to clear what you do, and don't assign an object by accident.
Concatenating a SQL string is insecure in case of SQL Injection. The object oriented approach isn't and it is type safe.
This should work as you expect:
Option Compare Database
Option Explicit
Sub AddItems()
Dim sql1 As String
sql1 = "SELECT a.*, b.YourDate, b.Weeks, b.Rate FROM TableA as a LEFT JOIN TableB as b On a.GroupId = b.Id ORDER BY b.YourDate, a.Id"
Dim db As DAO.Database
Set db = CurrentDb
Dim rst As DAO.Recordset
Set rst = db.OpenRecordset(sql1)
rst.MoveFirst
While Not rst.EOF
If rst("Weeks").Value > 0 Then
Dim i As Long
For i = 1 To rst("Weeks").Value
With CurrentDb().CreateQueryDef(vbNullString, _
"INSERT INTO TableC ([ID], [CUSTOMER], [YourDATE], [AMOUNT]) " & _
"VALUES (#ParID, #ParCustomer, #ParDate, #ParAmount)")
.Parameters("#ParID").Value = rst("ID").Value
.Parameters("#ParCustomer").Value = rst("CUSTOMER").Value
.Parameters("#ParDate").Value = DateAdd("ww", (i - 1), rst("YourDate").Value)
.Parameters("#ParAmount").Value = rst("AMOUNT").Value
.Execute dbFailOnError
End With
Next i
End If
rst.MoveNext
Wend
rst.Close
End Sub
Date values are not stored with a format if the data type is Date. A format is for display only or when concatenating in SQL. So try:
Sub AddItems()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim Sql1 As String
Dim Sql2 As String
Sql1 = "SELECT [a].*, [b].[Date], [b].[Weeks]. [b].[Rate] FROM TableA as [a] LEFT JOIN TableB as [b] WHERE [a].[GroupId] = [b].[Id] ORDER BY [b].[Date], [a].[Id];"
Set db = CurrentDb
Set rst = db.OpenRecordset(Sql1)
rst.MoveFirst
While Not rst.EOF
If rst![Weeks] > 0 Then
For i = 1 to rst![Weeks]
Sql2 = "INSERT INTO TableC ([ID], [CUSTOMER], [DATE], [AMOUNT]) VALUES ("
Sql2 = Sql2 & rst![ID] & ", '" & rst![CUSTOMER] & "', "
Sql2 = Sql2 & "#" & Format(DateAdd("ww", i - 1, rst![Date]), "mm/dd/yyyy") & "#, "
Sql2 = Sql2 & Str(rst![AMOUNT])
Sql2 = Sql2 & ")"
Debug.Print Format(DateAdd("ww", i - 1, rst![Date]), "mm/dd/yyyy")
db.Execute(Sql2)
Next i
End If
rst.MoveNext
Wend
End Sub
That said, it would be much simpler and faster to open a second recordset for TableC and then use AddNew and Update to append the records for the weeks.
If your "dates" are stored as text, there is no way to solve your issue other than manual editing to bring them into a consistent format.

Access pass-through query with parameters not updating with prompted parameters

An Access pass-through query works when using the default parameters. When used in an Access report, the prompts that are used returns records based on the default parameters in the ptq and not the answered prompts. Default data is being returned.
I have a SQL Server based stored procedure that works, uspWorkCentreReport, that uses #TheDate DATE, #WC VARCHAR(15), #Shift INT for parameters and returns, through a SELECT statement, these columns:
[JOB NUMBER], [REL #], [JOB NAME], QTY.
Here's the ALTER line of the stored procedure code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport]
#TheDate DATE,
#WC VARCHAR(15),
#Shift INT
The Access pass-through query, ptq_uspWorkCentreReport, passes these default parameters '2019-05-30','PCOT',1 and uses a DSN-less ODBC connection that works to return default data. I forgot to try but I think it will return correct data with whatever default parameters I use to replace '2019-05-30','PCOT',1. EDIT - I tried it this morning and indeed any appropriate replacement parameters return the appropriate associated records. Here's the ptq's one line:
exec uspWorkCentreReport '2019-05-30','PCOT',1
I provide the ptq with default parameters based on Albert D. Kallal's SO reply.
I use an Access select query, qry_ptq_uspWorkCentreReport, to receive [JOB NUMBER],[REL #],[JOB NAME],QTY and pass the parameters TheDate, set to Date With Time, WC, set to Short Text, and Shift, set to Integer.
qry_ptq_uspWorkCentreReport uses the pass-through query. The parameters are set using Access' Parameters applet and not within the query fields. Running this select query prompts for the 3 parameters but only returns data based on the default parameters set in the ptq's one line. I did not think to look at the Access SQL statement but will do so when I get to work tomorrow morning. EDIT - Here's the SQL statement for qry_ptq_uspWorkCentreReport:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
SELECT ptq_uspWorkCentreReport.[JOB NUMBER], ptq_uspWorkCentreReport.[REL #], ptq_uspWorkCentreReport.[JOB NAME], ptq_uspWorkCentreReport.QTY
FROM ptq_uspWorkCentreReport;
Of course the above three functions culminate in an Access report, rpt_qry_ptq_WorkCentreReport to make the records human readable.
I have used the same scenario for another report the takes From and To dates as parameters. When that report runs, the prompts take the dates and return records based on those dates and not the dates in the ptq. Here's that ptq:
exec uspMergeAandPJobs '2018-01-01','2019-01-01'
Indeed, I tried using
exec uspMergeAandPJobs '',''
And the report returns 0 records!
Not sure what I am missing and would appreciate any feedback. TIA.
I tried the following with the help of a tutor:
Sub Report_Load()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.SQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
qdf.Connect = "ODBC;DRIVER=ODBC Driver 13 for SQL Server;SERVER=OURS\NTSQL;Trusted_Connection=Yes;DATABASE=TablesCoE;ApplicationIntent=READONLY;"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub
After the prompts VBA spits up a Run-Time error 3129 - Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. Neither of us were able to determine what was causing the error. In VBA the "qdf.SQL..." line is highlighted in yellow.
EDIT - Adding stored proc's SQL code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport_TEST] #FromDate DATETIME,#ToDate DATETIME,#WC VARCHAR(15),#Shift INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Build table variable SumTable structure
DECLARE #SumTable TABLE(matl_nbr VARCHAR(60),QTY DECIMAL(4,0),matl_dsc VARCHAR(50))
-- P jobs and their summed WorkCentre traversals using crosstab - each traversal is added up
INSERT INTO #SumTable(matl_nbr,matl_dsc,QTY)
SELECT SRC1.matl_nbr,SRC1.matl_dsc,
SUM(CASE WHEN SRC1.locn_to = #WC THEN 1 ELSE 0 END) AS QTY
FROM
(
SELECT matl_nbr,matl_dsc,locn_to
FROM mtrk_CompanyE.dbo.trxn_hstd th
WHERE (last_upd >= #FromDate AND last_upd <= #ToDate) AND
locn_to = #WC
)SRC1
GROUP BY matl_nbr,matl_dsc
-- These updates take all the summed WorkCentre (locn_to) columns and turn each into "1" for later summing
UPDATE #SumTable
SET QTY = 1
WHERE QTY >1
-- Shortening the material number from 123456_00_00_R1_00 to 1234560
UPDATE #SumTable
SET matl_nbr = LEFT(matl_nbr,6) + right(LEFT(matl_nbr,9),1)
SELECT LEFT(A.matl_nbr,6)[JOB NUMBER],SUBSTRING(A.matl_nbr,7,1)[REL #],matl_dsc AS [JOB NAME],QTY
FROM (SELECT matl_nbr,matl_dsc,
SUM(CASE WHEN QTY = 1 THEN 1 ELSE NULL END) AS QTY
FROM #SumTable
GROUP BY matl_nbr,matl_dsc)A
ORDER BY QTY DESC;
END
EDIT - Finished sub:
Private Sub Report_Open(Cancel As Integer)
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("Enter From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport_TEST " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
DoCmd.OpenReport "rpt_qry_ptq_uspWorkCentreReport", acViewReport
Me.lblFromDate.Caption = strFromDate
Me.lblToDate.Caption = strToDate
Me.lblWC.Caption = strWC
Me.lblShift.Caption = intShift
End Sub
Your Access query has parameters:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
and since they are defined in the query definition, Access asks for them when opening/running the query.
But these parameters are never used!
There is no way for Access to pass these parameters into the pass-through query that is the basis of the Access query. Again, a PT query is nothing more than a Connect string and a constant SQL string.
So when you run the Access query, it will always run the saved contents of the PT query, i.e.
exec uspWorkCentreReport '2019-05-30','PCOT',1
The parameters you entered are ignored.
What you need to do (as outlined in the answer you refer to):
create a form to collect the parameter values
dynamically create the SQL string for the PT query with VBA
assign that SQL to the PT query:
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSql
(it is automatically saved)
and then you can run the report based on the Access query - or better: directly use the PT query as record source for the report.
Remove the parameters from the Access query, they are of no use for your situation. Or remove the query entirely, unless you need it to join the PT query with something else.
Edit for above edit:
If you get a runtime error, there is probably a syntax error in your .Sql. Build the SQL string in a variable, do Debug.Print strSql, and run that string in SSMS. You may need to change date formatting (depending on your locale settings).
Also: See my 3rd bullet. Defining a temporary querydef and opening a recordset doesn't work for a report. You must assign the .Sql of the existing query that is the record source of the report.
Addendum: if you need to create a new query, first set .Connect, and then .Sql, so Access knows it's a Pass-Through query.
Access SQL doesn't know exec.
Edit 2
You have an existing, working PT query ptq_uspWorkCentreReport, which returns records for one set of parameters, e.g.
exec uspWorkCentreReport '2019-05-30','PCOT',1
Use this query as record source for your report.
To run the report with different parameters, you must modify the query's SQL. You can do this manually in query design view, or with VBA.
I think Report_Load() is too late for modifying its record source (the PT query). Run the following sub, then open the Report.
Sub SetUspParameters()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
Debug.Print strSQL
' This line is all that's needed to modify the PT query
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
End Sub
In practice, you don't want to use 4 x InputBox, but a form.

Error in ConcatRelated

Here is my table structure:
ID1 - autonumber
TDate - Date/Time
TTime - Date/Time
TID - Text
TMessage - Text
Tcode - text
TMessage value can be 100+ characters. There can be more than 1 TMessage for each TID
Sample input:
TDate TID TTime TMessage
1/9/2014 93378 12:28:28 PM aaaaaaaaaaaa
7/24/2014 02645 12:56:22 PM bbbbbbbbbbbb
7/24/2014 02645 12:56:30 PM cccccccccccc
Output should be:
TDate, TID, TMessage
1/9/2014 93378 aaaaaaaaaa
7/24/2014 02645 bbbbbbbbbb,cccccccccccc (the output can be more than 255 chars.)
This is my query/module (got from Stackoverflow and Allen Brown)
Query (SQL View)
SELECT sub.TDate, sub.TID, ConcatRelated("[TMessage]","RawData2","[TDate] = '" & sub.TDate & "' AND [TID] = '" & sub.TID & "'","[TDate], [TID]") AS concat_Message
FROM (SELECT q.TDate, q.TID FROM RawData2 AS q GROUP BY q.TDate, q.TID) AS sub
ORDER BY sub.TDate, sub.TID;
Function :
Option Compare Database
Public Function ConcatRelated(strField As String, _
strTable As String, _
Optional strWhere1 As Date, _
Optional strWhere2 As String, _
Optional strOrderBy1 As Date, _
Optional strOrderBy2 As String, _
Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
'Purpose: Generate a concatenated string of related records.
'Return: String variant, or Null if no matches.
'Arguments: strField = name of field to get results from and concatenate.
' strTable = name of a table or query.
' strWhere = WHERE clause to choose the right values.
' strOrderBy = ORDER BY clause, for sorting the values.
' strSeparator = characters to use between the concatenated values.
'Notes: 1. Use square brackets around field/table names with spaces or odd characters.
' 2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
' 3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
' 4. Returning more than 255 characters to a recordset triggers this Access bug:
' http://allenbrowne.com/bug-16.html
Dim rs As DAO.Recordset 'Related records
Dim rsMV As DAO.Recordset 'Multi-valued field recordset
Dim strSql As String 'SQL statement
Dim strOut As String 'Output string to concatenate to.
Dim lngLen As Long 'Length of string.
Dim bIsMultiValue As Boolean 'Flag if strField is a multi-valued field.
'Initialize to Null
ConcatRelated = Null
'Build SQL string, and get the records.
strSql = "SELECT " & strField & " FROM " & strTable
If strWhere1 <> vbNullString And strWhere2 <> vbNullString Then
strSql = strSql & " WHERE " & strWhere1 And strWhere2
End If
If strOrderBy1 <> vbNullString And strOrderBy2 <> vbNullString Then
strSql = strSql & " ORDER BY " & strOrderBy1 And strOrderBy2
End If
Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
'Determine if the requested field is multi-valued (Type is above 100.)
bIsMultiValue = (rs(0).Type > 100)
'Loop through the matching records
Do While Not rs.EOF
If bIsMultiValue Then
'For multi-valued field, loop through the values
Set rsMV = rs(0).Value
Do While Not rsMV.EOF
If Not IsNull(rsMV(0)) Then
strOut = strOut & rsMV(0) & strSeparator
End If
rsMV.MoveNext
Loop
Set rsMV = Nothing
ElseIf Not IsNull(rs(0)) Then
strOut = strOut & rs(0) & strSeparator
End If
rs.MoveNext
Loop
rs.Close
'Return the string without the trailing separator.
lngLen = Len(strOut) - Len(strSeparator)
If lngLen > 0 Then
ConcatRelated = Left(strOut, lngLen)
End If
Exit_Handler:
'Clean up
Set rsMV = Nothing
Set rs = Nothing
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
Resume Exit_Handler
End Function
Currently, this is the output:
TDate TID Concat_Message
1/9/2014 93378 #Error
7/24/2014 02645 #Error
7/24/2014 04533 #Error
Question 1 - What could be the reason for the Error? How to solve it?
Question 2 - Can I just create a table manually with Memo field so I can store the result of the Query to this table?

Unknown issue with insert statment

I have a sub form with staff records on it within a main form. I am trying to allow the user to select a record from the sub form and add it to a table, here is my code which, to me, looks correct. However it gives me an error saying "Syntax error in INSERT INTO"
Private Sub Command3_Click()
Dim dbs As Database
Dim sqlstr As String
Set dbs = CurrentDb
Forename = Nz(Forms!frm_Capex_Submission!frm_staffSub.Form.shy_forename, "")
Surname = Nz(Forms!frm_Capex_Submission!frm_staffSub.Form.shy_surname, "")
emp_no = Nz(Forms!frm_Capex_Submission!frm_staffSub.Form.shy_empno, "")
CAP_ID = Forms!frm_Capex_Submission!CAP_ID
sqlstr = "INSERT INTO tbl_CapexStaff ( Forename, Surname, EmployeeID, CAP_ID) )" _
& " SELECT '" & Nz(Me!shy_forename, "") & "' AS Expr1, '" & Nz(Me!shy_surname, "") & "' AS Expr2, '" & Nz(Me!shy_empno, "") & " AS Expr3, " & Forms!frm_Capex_Submission.CAP_ID & " as expr4, """
dbs.Execute (sqlstr)
tbl_CapexStaff.Requery
End Sub
There is an extra ")" in your query
INSERT INTO tbl_CapexStaff ( Forename, Surname, EmployeeID, CAP_ID) )

MS-Access 2003: Create multiple records from a single form

Can anyone point me to an example of an Access form which can create multiple records (in a single table) based on one form?
To expand: we're recording information about time spent on a project on a given date. We've had a request for a single form that would allow a user to enter data for 5 (or 7) days of a given week on a single form. He/she would pick a week from a calendar control, a project from a listbox, then enter up to 7 numbers for the hours spent that week.
I did check questions 5294128, which doesn't seem applicable, and question 8070706, which seems to imply that this can only be done in VBA (not using the GUI). Any alternatives?
Thanks.
Something on these lines should suit. This is an unbound form with a subform.
You can get the form type from the form wizard
To work properly, you will need a little code, say:
Private Sub cmdGo_Click()
Dim rs As DAO.Recordset
Dim sSQL As String
Dim sSDate As String
Dim sEDate As String
sSDate = "#" & Format(Me.txtStartDate, "yyyy/mm/dd") & "#"
sEDate = "#" & Format(Me.txtStartDate + Me.txtNoDays, "yyyy/mm/dd") & "#"
sSQL = "SELECT * FROM MyTable WHERE DataDate Between " & sSDate _
& " AND " & sEDate
Set rs = CurrentDb.OpenRecordset(sSQL)
If rs.RecordCount < Me.txtNoDays Then
AddRecords sSDate, sEDate
End If
Me.DataSubform.Form.RecordSource = sSQL
End Sub
Sub AddRecords(sSDate, sEDate)
''Uses counter table with integers from 0 to
''highest required number
''Another useful table is a calendat table, which would
''save some work here.
sSQL = "INSERT INTO MyTable (DataDate) " _
& "SELECT AddDate FROM " _
& "(SELECT " & sSDate _
& " + [counter.ID] AS AddDate " _
& "FROM [Counter] " _
& "WHERE " & sSDate _
& "+ [counter.ID] Between " & sSDate _
& " And " & sEDate & ") a " _
& "WHERE AddDate NOT In (SELECT DataDate FROM MyTable)"
CurrentDb.Execute sSQL, dbFailOnError
End Sub