I have a table named QB that has a field name Item. I'm trying to use find first on it with
Dim curDatabase As DAO.Database
Set curDatabase = CurrentDb
Set rsQB = curDatabase.OpenRecordset("QB", dbOpenDynaset)
'This works and equals the value for Item in the first record of the table
msgbox (rsQB("Item").Value)
'Item_No.Value is a string. The following all do not work:
rsQB.FindFirst "[Item] = " & Item_No.Value
rsQB.FindFirst ("[Item] = " & Item_No.Value)
rsQB.FindFirst "[Item] = '" & Item_No.Value &"'"
rsQB.Close
Related
I have this Access application where I pull in data from a SQL server database where the date is saved as a text field. I just created the ability for the users to sort the Recordset. It is very easy for text fields but now I want to be able to sort by date but unfortunately, as I said earlier it is saved as text in this format mm-dd-yyyy.
Here is the sort code I use:
Dim strSQL As String: strSQL = "exec Liab" & strQuearyName & " '" & strSearchValue & "%' "
'Dim qdf As QueryDef: Set qdf = CurrentDb.QueryDefs(strQuearyName)
With CurrentDb.QueryDefs(strQuearyName)
.SQL = strSQL
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
'SortOutput rst
'rst.Sort = DateValue("DateOfLoss")
rst.Sort = Forms("SelectLiabilityClaimForm").cobSortBy
Set rstSorted = rst.OpenRecordset
With Forms("SelectLiabilityClaimForm").lstbxClaimList
'.RowSource = queryName
Set .Recordset = rstSorted
'.OrderBy = "claimantName"
.Requery
.SetFocus
End With
How can I sort by the text date, without having to change the passthrough query (I want that as a last resort)
I have a query called 'qryAddressBook'. I want to be able to loop vertically through the records in a specific field called 'Client_Address' and display those records on a single row in a table 'tblClient'.
I have done this in the past with tables using "Set rs = dbs.OpenRecordset("tblAddressBook", dbOpenTable)" with no issues....
...and I followed the syntax showed from Access Database.OpenRecordset method (DAO): https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/database-openrecordset-method-dao
I continue to get an error that says, 'Object variable or With block variable not set' and it highlights the following text from my code:
Set rs = dbs.OpenRecordset("qrySpecificNCR", dbOpenDynaset)
Here is my total code:
DoCmd.OpenQuery "qryAddressBook"
Dim dbs As DAO.Database
Dim rs As Recordset
Dim SeqNum As Integer
Set dbs = CurrentDb
SeqNum = 1
Set rs = dbs.OpenRecordset("qryAddressBook", dbOpenDynaset)
Do Until rs.EOF
Dim srtAddress As String
srtAddress = rs.Fields("Client_Address").Value
Dim strSQLAddress As String
strSQLAddress = "UPDATE tblClient SET " & SeqNum & " = '" & srtAddress & "' WHERE Record = 1;"
DoCmd.RunSQL strSQLAddress
SeqNum = SeqNum + 1
rs.MoveNext
Loop
I still don't understand the explicit part...but I found the error.
My qryAddressBook had the following line of code:
FROM qryGlobalAddress
WHERE (((qryAddress.Client) = [FORMS]![frmClientAddress]![CmbClientName]));
I was pushing a form parameter from a combo box. When I changed this to a specific client name such as:
FROM qryGlobalAddress
WHERE (qryAddress.Client) = 'Smith, John';
Then the Set rs = dbs.OpenRecordset("qryAddressBook", dbOpenDynaset) worked perfectly.
The problem now is I'll need to figure out a way to push a form parameter to the query. :(
cancatenate the form combo value
"FROM qryGlobalAddress WHERE (((qryAddress.Client) = '" & [FORMS]![frmClientAddress]![CmbClientName] & "'));"
I have 2 fields - txtTR1_Unit and cmbTR2_Unit. Together, these 2 fields represent the total UNIT.
cmbTR2_Unit has a list of unique values that when selected - txtTR1_Unit automatically gets the related value.
I've created a function called Tier1from2 - that accepts a 'string' and returns the related Tier1 value.
So when I update cmbTR2_Unit in my After_Update event, I'd like to automatically tab to the next field. - Another combo box. I figured that I shouldn't need to set any focus, because it would automatically go to the next field after updating.
txtTR1 gets updated just as expected from my Function, but then it just sits there and won't go to the next field. So I have attempted to 'SetFocus' to the next field after the update.
Still no go. What did I miss??
Private Sub cmbTR2_UNIT_AfterUpdate()
If Len(Me.cmbTR2_UNIT.Value) <> 0 Then
Me.txtTR1_UNIT.Value = Tier1From2(Me.cmbTR2_UNIT.Text)
'cmb_CostCenter.setfocus - 'this doesn't seem necessary - but it doesn't work anyway.
End If
End Sub
As a test I tried removing the function "Tier1From2(Me.cmbTR2_UNIT.text)" simply hard coding the word 'RESULT' in txtTR1_UNIT and it works without a hitch. I know I used to write a more simple function but I haven't touched VBA in awhile - How can I simplify this function:
Private Function Tier1From2(strTier2 As String) As String
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim strQry As String
Dim rs As Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs("qUNIT_HUB")
strQry = "SELECT Tier1_Unit, Tier2_Unit " & _
" FROM LTBL_Cost_Collector " & _
" GROUP BY Tier1_Unit, Tier2_Unit " & _
" HAVING (((Tier2_Unit) = '" & strTier2 & "'));"
qdf.SQL = strQry
db.QueryDefs.Refresh
Set rs = db.OpenRecordset(strQry)
Tier1From2 = rs![Tier1_Unit]
Set db = Nothing
Set qdf = Nothing
Set Recordset = Nothing
End Function
It turns out that something in this function was causing the field and form to loose focus. db.QueryDefs.refresh perhaps? The solution was to update my Function as follows
Private Function Tier1From2(strTier2 As String) As String
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim strSQL As String
Dim strTier1 As String
Set db = CurrentDb
strSQL = "SELECT Tier1_Unit, Tier2_Unit " & _
" FROM LTBL_Cost_Collector " & _
" GROUP BY Tier1_Unit, Tier2_Unit " & _
" HAVING (((Tier2_Unit) = '" & strTier2 & "'));"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
strTier1 = rs!Tier1_Unit
Set rs = Nothing
Set db = Nothing
Tier1From2 = strTier1
End Function
This worked without a hitch.
I have a subform bound to a SQL statement. Inside the subform, I have a few text boxes bound to the fields of this SQL. However, I have another text box that needs to be bound to a field from a different SQL statement with criteria from the first one. My code looks like below:
Dim subform As Object
Dim formFilter As String
formFilter = "SELECT * FROM my_table_1"
Set subform = Me!my_subform.Form
subform.RecordSource = formFilter
subform.field1.ControlSource = "tb1f1"
subform.field2.ControlSource = "tb1f2"
...
subform.f3.ControlSource = "= SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4"
I cannot use a DLOOKUP function here directly, because I need to sort the table result.
Thanks in advance for your help.
I think I would simply create a little function to go get the result you want. It would probably be best to simply rework DLookup in your own function and add sort but I won't do that here.
First the form code. Notice I am not setting the recordsource, just the value which may not be what you want.
subform.f3 = fGet_tb2f3([tb1f1], [tb1f2])
Then the function code (put in your own error handling)
Public Function fGet_tb2f3(tblf1 as String,tblf2 as String) as String
Dim rst as dao.recordset
Dim db as database
set db = currentdb
set rst = db.openrecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & tb1f1 & "' AND tb2f2 = '" & tb1f2 "' ORDER BY tb2f4",dbopensnapshot
if not rst is nothing then
if not rst.eof then fGet_tb2f3 = rst!tb2f3
rst.close
set rst = nothing
end if
db.close
set db = nothing
end Function
You can't bind controls on the same form to 2 different recordsets. The only thing you could do is pull data from 2 different recordsets, but that's probably not the best way to do anything.
In order to do that, you'd have to create a second recordset and grab that value in it. Something like:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4")
Me.MyControlName = rec(0)
I am working on some VBA macros. I also use ADODB.Recordset to access data in my Access database.
Now, I need to loop through records in my table and in this loop, also need to update the same table in another function.
Here is the code (cleaned for easy reading).
Dim strSql As String
Dim rstCycles As adodb.Recordset
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & dteCurrentDate & "#"
Set rstCycles = SelectQuery(strSql)
While Not rstCycles.EOF
if ... then
rstCycles("NoCycle1") = ...
rstCycles.Update
end if
RefreshPlanning (*)
rstCycles.MoveNext
Wend
(*) In this function I perform a select on the tblCycles table.
The problem is after the rstCycles.Update, the data is not immediately record on disk thus the call to RefreshPlanning does not read updated data. If I set a '1 second pause' after the update everything is ok. I don't want to use a kind of pause in my loop. Is there another solution?
UPDATE ---------------
RefreshPlanning is a simple function to refresh my excel sheet based on my tblCycles table.
Sub RefreshPlanning(DatePlanning As Date, CodeEquipement As String)
Dim strSql As String
Dim rstCycles As adodb.Recordset
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & DatePlanning & "# AND CodeEquipement = '" & CodeEquipement & "'"
Set rstCycles = SelectQuery(strSql)
While Not rstCycles.EOF
' Some code here to update my excel sheet
' ...
rstCycles.MoveNext
Wend
End Sub
DAO is many times faster than ADO with MS Access:
Dim strSql As String
Dim rstCycles As DAO.Recordset
Dim db As Database
Set db = OpenDatabase("z:\docs\test.accdb")
strSql = "SELECT * FROM tblCycles WHERE DatePlanning = #" & dteCurrentDate & "#"
Set rstCycles = db.OpenRecordset(strSql)
While Not rstCycles.EOF
if ... then
rstCycles.Edit
rstCycles("NoCycle1") = ...
rstCycles.Update
end if
''Need more information here
RefreshPlanning (*)
rstCycles.MoveNext
Wend
If dteCurrentDate is the same as today, you can say:
"SELECT * FROM tblCycles WHERE DatePlanning = Date()"