Access Form checkbox to update date in another table - vba

I'm trying to make a form checkbox update a date in another table.
Private Sub Delivered_AfterUpdate()
If Delivered = -1 Then
[tool implentation].[date] = Now()
End Sub
I'm getting an error on the [tool implentation].[date] = Now() line.
I would like if any time the checkbox is clicked (check and un-check) the date is updated.

I'm assuming your table is also the form's recordset here. If it is not you will need something to determine which record in the table you want to edit.
Private Sub Delivered_AfterUpdate()
If Me.Delivered = True Then
With Me.Recordset
.Edit
![ImplementationDate] = Now()
.Update
End With
End If
End Sub
You can leave Me.Delivered = -1 if you want but I find True/False to be easier to read.
I also recommend not naming something Date, I'm not sure if it actually matters as a column name, but it is both a builtin function and data type. So I changed it.
You should also indent your code, it will be a nightmare as you get more lines.

Related

MS Acces - select case mixing tables

I have problem with VBA code select case. Code is linked to combobox(Combo0) and event after update. In combobox there is a list of values: BHP, PPOZ, ISO. When i choose BHP, it should change source object property in subfrom(Child03) to subfrmBHP. Same with PPOZ and ISO. But for some reason, when i choose BHP it shows table from PPOZ, when i choose PPOZ it shows BHP and when i choose ISO it shows BHP. Code looks like this:
Private Sub Combo0_AfterUpdate()
Select Case ChangeCombo
Case Combo0 = "BHP"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmBHP"
Case Combo0 = "PPOŻ"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmPPOZ"
Case Combo0 = "ISO"
Forms("frmInstrukcje").Form.Child3.SourceObject = "subfrmISO"
End Select
End Sub
I aleredy recreated tables, form, sub form. Someone suggested me it could be data binding in subform, but there is nothing written there.
Your method references two controls, ChangeCombo and Combo0. Not sure if it's a typo or you have something else in mind.
Anyway, Case Combo0 = "BHP" is incorrect, so assuming Combo0 is the correct control, try the below.
Private Sub Combo0_AfterUpdate()
With Forms("frmInstrukcje").Form.Child3
Select Case Me.Combo0.Value
Case "BHP":
.SourceObject = "subfrmBHP"
Case "PPOŻ":
.SourceObject = "subfrmPPOZ"
Case "ISO":
.SourceObject = "subfrmISO"
End Select
End With
End Sub

Is There a way to sort by .column of a drop down field in VBA

I want to sort a form by two fields: [datakey] and [employeenumber].column(1) and I cannot get it to work. Is there a way to do this?
No.
You can only sort by columns that are member of the form's recordsource.
Private Sub Form_Load()
Me.OrderBy = "datakey, employeenumber"
Me.OrderByOn = True
End Sub
You will have to join the field you use in Column(1) in the record source of the form itself. Then you can sort by this field.

comparing a table and a query

I have a form, a table ("my_table") and a query ("my_query").
I want a function to look if any of the table's record's IDs ("my_ID") match with the IDs of the query (also "my_ID") to update a field ("my_Property") of given record with a value from the form.
I copied and modified this code. My code causes an error (have to translate): "Runtime error '3061': 1 parameter was expected, but too few parameters were passed."
I think the issue is that in the if loop I don't compare tbl.Fields("my_ID") with a discreet value but with a set values. Maybe I should also iterate through all values of qry.Fields("my_ID") but I don't see how to do the code. Also, this would significantly slow down the already slow process since my_Table contains more than 40,000 records. Is there a faster method of comparing the ids?
Private Sub btn_Click()
Dim db As Database
Set db = CurrentDb
Dim tbl As Recordset
Set tbl = db.OpenRecordset("my_Table")
Dim qry As QueryDef
Set qry = db.OpenRecordset("my_Query")
tbl.MoveFirst
Do Until tbl.EOF
If tbl.Fields("my_ID") = qry.Fields("my_ID") Then
tbl.Edit
tbl.Fields("my_Property") = Me!textbox1.Value
tbl.Update
End If
tbl.MoveNext
Loop
End Sub
As #Harassed Dad and #Andre suggested I simply copied "my_query" and changed the copy to an update query "my_updatequery" with the update field of "my_Property" set to
[Forms]![My_Form]![tbx_my_Value]. Then I changed the code for the button to:
Private Sub btn_Click()
DoCmd.OpenQuery "my_updatequery"
Forms.my_Form.Subform2.Requery
End Sub
Forms.my_Form.Subform2.Requery updates basically a subform showing the "my_query"-query.
I will look further for another way to call the update query to avoid the two prompts that opening an update query always entails.
edit:
To to avoid the propmts I simply turned Warnings off and on again:
Private Sub btn_Click()
DoCmd.SetWarnings False
DoCmd.OpenQuery "my_updatequery"
DoCmd.SetWarnings True
Forms.my_Form.Subform2.Requery
End Sub
Not the most elegant way but it will do the job.

Reference table and form text box MS Access VBA

Pardon the code below. I am a VBA novice. I am looking to upon clicking a form button, inform customers that they must make another library selection if the item they have chosen is checked out. I determine something is checked out if the "Check In Date" for the most recent date in the "1Transaction" table is NULL. Note for every check in and check out, a new record is created in the 1Transaction table and every record (whether check in or check out) will have the check out date info. So the logic, take the most recent date for the lease (book) number and if there is no return date then it is still checked out. The code below is meant to make the references and return a message box in VBA but I am stuck. I understand logically what I require but I know my VBA syntax is very off. Thanks.
Private Sub Check_Out()
If [1Transactions].[Asset].Value = Me.Lease_Num
And DMax([Tables]![1Transactions].[Check Out Date])
And [Tables]![1Transactions].[Check In Date] = NULL
Then MsgBox "The requested documents are currently checked out"
End If
DoCmd.OpenForm "Check In"
End Sub
Note:
1Transactions = Table holding all check in/out data
Me.Lease_Num = value pulled from combo box that user fills out to provide "lease number" (book code) they are interested in checking out.
Okay - instead of trying to modify every line of your code, I think it's better to use a Parameter Query to simple check if the item is out now
First create a query using your transactions table - modify the SQL to below
and save it as "qdfLease"
PARAMETERS [What Lease Num] Text ( 255 ); SELECT TOP 1
[1Transactions].Asset, [1Transactions].[Check Out Date],
[1Transactions].[Check In Date] FROM 1Transactions WHERE
([1Transactions].Asset = [What Lease Num]) And ([1Transactions].[Check
In Date] Is Null) ORDER BY [1Transactions].[Check Out Date] DESC;
Modify the code in your sub to:
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strLeaseNum As String
strLeaseNum = nz(Me.Lease_Num,"")
Set qdfLease = CurrentDb.QueryDefs("qdfLease")
qdfLease.Parameters("What Lease Num") = strLeaseNum
Set rs = qdfLease.OpenRecordset(dbOpenDynaset, dbReadOnly)
If rs.EOF Then
' Item is Checked In
Else
' Item is Checked Out
End If
rs.Close
Set rs = Nothing
You have [1Transactions].[Check Out Date].Value = Me.Lease_Num
Is that what you want to compare the lease_num to? It sounds like it should be a different field name in [1Transactions]
I would change this
[Tables]![1Transactions].[Check In Date] = NULL
to this
IsNull([Tables]![1Transactions].[Check In Date])
What are you trying to compare here
DMax([Tables]![1Transactions].[Check Out Date])

Checking for date overlap across multiple date range objects

I have several records in a database that have Start and End Dates
09/15/2011 - 09/30/2011
10/15/2011 - 10/22/2011
11/01/2011 - 11/15/2011
When user stores a record, I need to make sure dates don't overlap.
My simple code checks date ranges within a specific record (e.g. user enters 9/16/2011 or 10/21/2011, I throw an exception.)
But, on the slim chance a user gets creative (e.g. 10/14/2011 - 10/23/2011 or even 10/14/2011 to 11/16/2011), now they have circumvented my check.
BTW, the user could enter 10/14/2011 to 10/23/2011 if they were editing the record that contained values 10/15/2011 - 10/22/2011.
So, I'm trying to solve this riddle with a linq query. However, what I have isn't working exactly right.
UPDATE Nevermind about code not working. While trying to provide an example to expand on Miika's repsonse, I found my answer. So, giving credit to Miika for pointing me in the right direction and posting my working code below:
Here's my code:
Private Sub CheckForOverlap(myMonth As Messages.MyMonth)
Dim am As New MyMonth()
Dim amCollection As Messages.MyMonthCollection
Dim overlappingMyMonthDate As Boolean = False
Dim sErrorMsg As String = ""
'...non-applicable code omitted
Dim query = From s In amCollection _
Let s1 As MyMonth = CType(s, MyMonth) _
Where s1.AttendanceMonthID <> attendanceMonth.AttendanceMonthID And _
(CDate(attendanceMonth.StartDate) < CDate(s1.StartDate) And CDate(attendanceMonth.EndDate) > CDate(s1.EndDate)) _
Select s1
If query.Count > 0 Then
sErrorMsg = "Dates entered surround another entry"
End If
If overlappingMyMonthDate Then
Throw New Exception(sErrorMsg)
End If
End Sub
End Class
It all came down a LINQ query.
Do you need to do it in code or would SQL be an option? If the data is in a database, you could use the following query to check for overlaps.
SELECT COUNT(*)
FROM Table1
WHERE Table1.StartDate < 'endCheckDate'
AND Table1.EndDate > 'startCheckDate'
This will return a count of the number of overlaps found. 'endCheckDate' and 'startCheckDate' are your new query values (in date format). If your data is in a object collection in memory, then you could use LINQ. If you need help with a LINQ statement, let me know.