Access sActualFieldValue - ms-access

I am working on a database that was created years ago by a colleague and updated most recently by a software designer who in his words has "bowed out" of servicing it as our database has become too complex.
I've received the following Run-time error '13": Type Mismatch.
The hi-lighted error is:
Me.Controls("Label" & i & "Desc").Caption = DLookup("[Description]", "tbl_Contract_Items", "[Mat_Item_No]='" & sActualFieldValue & "'")
It is a billing database, we bill our customer with item numbers like 1.23, 2.31, 24.02.03 and 21.03.15. Items with the single decimal work fine but it is the items with two decimals that cause the error. ie 24.02.03. The issue is new and was not a problem until recently.
Private Sub Report_Open(Cancel As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim fld As Field
Dim sActualFieldValue
Set db = CurrentDb()
Set rs = db.OpenRecordset("qxt_tbl_WEC_Qty_Crosstab1")
For i = 1 To rs.Fields.Count - 1 'rs(0) is Date
If i > 14 Then Exit For
sActualFieldValue = Replace(rs(i).Name, "_", ".", , 1)
Me.Controls("Text" & i).ControlSource = rs(i).Name
Me.Controls("Label" & i).Caption = sActualFieldValue
Me.Controls("Label" & i & "Desc").Caption = DLookup("[Description]", "tbl_Contract_Items", "[Mat_Item_No]='" & sActualFieldValue & "'")
Me.Controls("Text" & i & "Tot").ControlSource = "=sum([" & rs(i).Name & "])"
Next
'Stop
End Sub

Property Caption must be a string, but DLookup returns Null for not found. So wrap in Nz():
Me.Controls("Label" & i & "Desc").Caption = Nz(DLookup("[Description]", "tbl_Contract_Items", "[Mat_Item_No]='" & sActualFieldValue & "'"))

Related

Openrecordset With Multiple Tables, Access 2016

I am getting an "Object variable or With block variable not set (Error 91)" error with the following code:
Dim db As Database
Dim rs As Recordset
Private Sub Form_Load()
Dim mySQL As String
mySQL = "SELECT Tuteurs.ID_Tuteur, Tarifs_17_18.*, Paiements_17_18.* " & _
"FROM (Tuteurs INNER JOIN Tarifs_17_18 ON Tuteurs.ID_Tuteur = Tarifs_17_18.TuteurID_Trf) " & _
"INNER JOIN Paiements_17_18 ON Tuteurs.ID_Tuteur = Paiements_17_18.TuteurID_Pmt " & _
"WHERE ID_Tuteur =" & [Forms]![Eleves]![TuteurID_Elv]
Set db = CurrentDb
Set rs = db.OpenRecordset(mySQL, dbOpenDynaset, dbSeeChanges)
rs.MoveFirst
End Sub
Private Sub btn_Enregistrer_Click()
Dim totIns As Integer
totIns = DSum("Montant", "Paiements_17_18", "[Mois_Regle]='Inscription'")
If totIns = rs!Tarif_Inscription Then
MsgBox "Yes" & totIns & " = " & rs!Tarif_Inscription
Else
MsgBox "No" & totIns & " # " & rs!Tarif_Inscription
End If
End Sub
totIns is working very well but rs!Tarif_Inscription is the missing object variable.
[Tarif_Inscription] is a field in the [Tarifs_17_18] Table.
Any Help Please?
I Found the Solution,
My recordset is not in scope and My variables are declared outside My procedure.
Solution found by #Moke123.

ms access findfirst goes to new record instead of displaying specified record

This code was working fine then suddenly starting displaying 'new' instead of the specified record. I can't think of anything I did that would make this suddenly stop working as expected. Any help would be greatly appreciated.
Private Sub cboHotel_AfterUpdate()
Dim rs As Object
Dim strCriteria As String
Set rs = Me.Recordset.Clone
strCriteria = "[EventName] = '" & Me.cboEvent & "' And [Hotel] = '" & Me.cboHotel & "'"
rs.FindFirst strCriteria
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
Forms!Submissions!TabCtl1.Visible = True
Forms!Submissions!EventName.SetFocus
End Sub
form>properties>data>data entry needs to be set to No

Access 2010 VBA - DAO Database connection "Operation is not supported for this type of object"

I'm really stuck. My coworkers and I cannot figure out why this database won't connect to "CurrentDb". Here's my code:
Dim db As Database, rs As DAO.Recordset
Dim strSQL As String, strRowSource As String
strSQL = "SELECT * FROM tbl_Documents"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
MsgBox "No Documents available!"
Exit Sub
End If
rs.MoveFirst
Do Until rs.EOF = True
strRowSource = strRowSource & rs!tbl_Documents.DocID & "," & rs!tbl_Document_Types.DocType & "," & rs!tbl_Documents.DocTypeID & "," & rs!tbl_Documents.DateReceived & "," & rs!tbl_Documents.LinkToFile & "," & rs!tbl_Documents.Comments & ";"
rs.MoveNext
Loop
Typically the error I get is "Item not found in this collection" during the Do Until loop. I put a watch on my database and recordset and it seems like neither are being set properly. I'm getting "Operation is not support for this type of object." in the connection field of the database object. Essentially, the exact same code is used for many other Access Databases that we have. Not sure why this won't play nice.
Looks to me like there are quiet a few changes that needs to be done to your code. As #OverMind has suggested, always declare the variables as they are. Specially libraries to avoid ambiguity in your code. Next, your strSQL includes only one table, but your strRowSource has another table. So your strSQL should be changed. I am not sure what the strRowSource does, but sounds to me like it is going to be a RowSource of a ListBox or ComboBox in that case, it is a bit confusing. Anyway your code should be.
Dim db As DAO.Database, rs As DAO.Recordset
Dim strSQL As String, strRowSource As String
strSQL = "SELECT * FROM tbl_Documents INNER JOIN tbl_Document_Types ON tbl_Documents.DocID = tbl_Document_Types.DocTypeID;"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
MsgBox "No Documents available!"
Exit Sub
End If
Do While Not rs.EOF
strRowSource = strRowSource & rs!DocID & "," & rs!DocType & "," & rs!DocTypeID & "," & rs!DateReceived & "," & rs!LinkToFile & "," & rs!Comments & ";"
rs.MoveNext
Loop
Now regarding your error. "Item not found in this collection" - could be because of the fact you were using the other fields which were not part of the recordset object. Try this code. Good luck. :)

How to filter and mail a part of my table from access into the body of my email

I am having some problems with my Access d-Base. I will try to explain what I want to achieve:
I have a continuous form based on a query with a lot of records. Users have to scan these records and If needed send a selection of these records to the concerning customer. Therefore I have used a textbox in my form combined with the following query column:
Expr1: (IIf([Forms]![frm_POOpenOrdersPerVendor]![partnrFilter] Is Null,True,[po_partnr]=[Forms]![frm_POOpenOrdersPerVendor]![partnrFilter]))
And criteria is: <>False
This works perfect in my form. After my filter is set I want to e-mail the results in the body of my email (Outlook). To do this I use:
Option Compare Database
Public Sub Export_PO()
On Error GoTo EH
'Recordset variables
Dim db As Database
Dim rstOpenPurchaseOrders As Recordset
Dim strSQL As String
Dim strSubject As String
Dim strBody As String
Dim strAddresses As String
Set db = CurrentDb()
strSQL = "SELECT * FROM qry_POOpenOrdersPerVendor;"
Set rstOpenPurchaseOrders = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rstOpenPurchaseOrders.EOF Then
strBody = "blah blah blah" & vbCrLf & vbCrLf
strBody = strBody & "Partnumber" & " | " & "Vendor Partnumber" & " | " & "Promise Date" & vbCrLf
strBody = strBody & "===========================================================" & vbCrLf
rstOpenPurchaseOrders.MoveFirst
Do While Not rstOpenPurchaseOrders.EOF
strBody = strBody & rstOpenPurchaseOrders![po_partnr] & Chr(9) & rstOpenPurchaseOrders![po_vendor_part] & Chr(9) & rstOpenPurchaseOrders![po_prom_date] & vbCrLf
rstOpenPurchaseOrders.MoveNext
Loop
End If
strSubject = "Here's my report!"
strAddresses = "look.look#nmhg.com"
DoCmd.SendObject acSendNoObject, , acFormatTXT, asAdresses, , , strSubject, strBody, True
Exit Sub
EH:
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Sub
Separately my codes work fine (both). If I want to combine them I get the error "3061 Too few parameters. Expected 1."
Anybody?
Thanks in advance!
Your Query qry_POOpenOrdersPerVendor - required one argument to be passed either via a Form or just by prompting for a value. In VBA if you wish to use a precompiled query that expects a parameter you will bump into this error. So you need to use the main Query to be used in the VBA NOT the name of the Query.

Cascading Combobox

Copy from: https://softwareengineering.stackexchange.com/questions/158330/cascading-comboboxes
ok so i have a form, in Access 2010, with 1 Textbox and 3 ComboBoxes (1 Enabled & 2 Disabled).
the first ComboBox is not tied to the datasource but is subjective to the other 2 comboboxes. So i handled the Click event for the first Combobox to then make the other 2 enabled, and preload the 2nd ComboBox with a custom RowSource SQL Script dynamically built based on the 1st ComboBox Value.
This all works great for New information but when i goto review the information, via Form, its back to the New mode on the controls.
Question:
What event do i need to handle to check if the current Form Data contains data for the Control Source of the Controls?
As i would express it in Logic (its a mix between C & VB, i know but should get the pt acrossed):
DataSet ds = Form.RowSet
if (ds = Null) then
cbo2.enabled = false
cbo3.enabled = false
else
cbo2.rowsource = "select id, nm from table"
cbo2.value = ds(3)
cbo3.value = ds(4)
end if
... do some other logic ...
Updated Logic - Still problem, cant catch for RecordStatus for some reason (gives 3251 Run-Time Error)
Private Sub Form_Current()
Dim boolnm As Boolean: boolnm = (IsNull(txtName.Value) Or IsEmpty(txtName.Value))
Dim booltype As Boolean: booltype = IsNull(cboType.Value)
Dim boolfamily As Boolean: boolfamily = IsNull(cboType.Value)
Dim boolsize As Boolean: boolsize = IsNull(cboType.Value)
Dim rs As DAO.Recordset: Set rs = Me.Recordset
MsgBox rs.AbsolutePosition
' If rs.RecordStatus = dbRecordNew Then
' MsgBox "New Record being inserted, but not committed yet!", vbOKOnly
' Else
' MsgBox rs(0).Name & " - " & rs(0).Value & vbCrLf & _
' rs(1).Name & " - " & rs(1).Value & vbCrLf & _
' rs(2).Name & " - " & rs(2).Value & vbCrLf & _
' rs(3).Name & " - " & rs(3).Value
' End If
'MsgBox "Name: " & CStr(boolnm) & vbCrLf & _
"Type: " & CStr(booltype) & vbCrLf & _
"Family: " & CStr(boolfamily) & vbCrLf & _
"Size: " & CStr(boolsize), vbOKOnly
End Sub
Here is the final result, with Remou's assistance, and this is only a precursor to the end result (which is out of the context of the question).
Private Sub Form_Current()
If Me.NewRecord Then <=======================
cboType.Value = 0
cboType.Enabled = True
cboFamily.Enabled = False
cboSize.Enabled = False
Else
Dim rs As DAO.Recordset: Set rs = Me.Recordset
'get Family ID
Dim fid As String: fid = rs(2).Value
'Build SQL Query to obtain Type ID
Dim sql As String
sql = "select tid from tblFamily where id = " & fid
'Create Recordset
Dim frs As DAO.Recordset
'Load SQL Script and Execute to obtain Type ID
Set frs = CurrentDb.OpenRecordset(sql, dbOpenDynaset, dbReadOnly)
'Set Type ComboBox Value to Type ID
cboType.Value = frs(0)
cboType_Click 'Simulate Click Event since the Value has changed
'Make sure all 3 Comboboxes are enabled and useable
cboType.Enabled = True
End If
End Sub