DLookup ControlSource - vba

Summary:
I want to display a value (in a text box) stored in another form upon choosing specific values from combo boxes.
I want to pass the two combo box values I have into the DLoopup property but every time I do so it gives me an error.
Below is the code is inserted in the control source property of a text box:
=DLookUp("[Year_ended]","1_Supportive_Housing","[BudgetYear] ='" & [Combo5] & "'")
This gives me an "#Error" in the text box.
Also tried the following but gives me "#NAME" error:
=DLookUp("[Year_ended]","1_Supportive_Housing","[BudgetYear] = '" & [Combo5.Value] & " And [Program_Name] = '" & [Combo7.Value] & "'")

You need to get your delimiters sorted out. AFAIR Budget Year is a number:
=DLookUp("[Year_ended]","1_Supportive_Housing","[BudgetYear] =" & [Combo5])
When you are passing a value to a numeric type field, you do not use delimiters, for text, you use quotes, either 'Abc' or "Abc", for dates, use hash (#) #2012/11/31#.
=DLookUp("[Year_ended]","1_Supportive_Housing","[BudgetYear] =" & [Combo5] & " And [Program_Name] = '" & [Combo7.Value] & "'")

Related

Opening a report with multiple criteria

Can someone please guide me on this? When I use:
If Not IsNull(Me.filterLocation) And Not IsNull(Me.filterArea) Then
If Application.CurrentProject.AllReports("rptFiltered").IsLoaded =
False Then
DoCmd.OpenReport "rptFiltered", acViewPreview, , (("ColumnLocation =
'" & Me.filterLocation.Value & "'") And ("ColunmArea = '" &
Me.filterArea.Value & "'"))
Exit Sub
End If
I get a syntax error with the code above. It works if I remove:
And ("ColunmArea = '" & Me.filterArea.Value & "'"))
But I also need records with this column values to also show on the report.
There are a few issues with your code:
If a single expression traverses onto a new line, you will need to suffix the preceding line with a line continuation symbol, which, in VBA, is the underscore character (_) e.g.:
DoCmd.OpenReport "YourReport", _
acViewNormal
The and should be included in the where condition:
DoCmd.OpenReport "rptFiltered", acViewPreview, , _
"ColumnLocation = '" & Me.filterLocation.Value & "' AND ColumnArea = '" & Me.filterArea.Value & "'"
You can use the syntax highlighting of any code editor to help you to determine the elements of the code which form a string, and which lie outside of the string.
You have a typo in the name of one of your fields:
ColunmArea
' ^---------- I'm guessing this should be ColumnArea
Finally, the parentheses surrounding the where condition argument are not recommended (except when you want to force an argument to be passed by value).

display table data in a list box (access form)

I want display table data in a list box with this code:
Me.List_history.RowSource = "SELECT Date " & _
"FROM Leaves " & _
"WHERE CodePersonali = " & Nz(Me.CodePersonali.Value)
but the list box doesn't display anything
List_history = my list box name
Date = field name in leave table
Date is a reserved word (it's a function), so you shouldn't use it as column name. If you must, put it between square brackets [Date].
If CodePersonali is a text column, you need to put the value between ''.
That would be
Me.List_history.RowSource = "SELECT [Date] " & _
"FROM Leaves " & _
"WHERE CodePersonali = '" & Nz(Me.CodePersonali.Value) & "'"

Ms Access VBA - Trying to call data from a combo box but only if a field is empty

From the code below I want [KronosNO] to come from a database Combo box
but only if the [RTWDate] column is Null.
Set Rs = Application.CurrentDb.OpenRecordset(
"SELECT * FROM tblAbsence WHERE [KronosNo] = '" & Me.cmbKronosNo.Value & "'
AND [RTWDate] = '" & "Is Null" & "' ", dbOpenDynaset)
Don't treat Is Null like a variable, it is a part of SQL.
You simply need
AND [RTWDate] Is Null
in your string.

MS Access VBA to update table field with rich-text textbox value

I have a form with an unbound Rich Text textbox control (named: tbxNote). I am using the following code to update the target table (named: tblSupeGenNotes) with the values from various controls on the form:
dbs.Execute "UPDATE tblSupeGenNotes " & _
"SET [NoteDate] = #" & Me.tbxNoteDate & "#, " & _
"[SupeType] = " & Me.cbxSupeType & ", " & _
"[SupeAlerts] = " & alrt & ", " & _
"[Note] = " & Chr(34) & Me.tbxNote & Chr(34) & " " & _
"WHERE [SupeGenNoteID] = " & Me.tbxSupeGenNoteID & ";"
All data is getting into the target table in the correct fields with the correct values except that the [Note] field that pulls its data from the Rich Text textbox control (me.tbxNote) is including HTML tags in the table field result. For example, I get the following: "<<div>>Sample! {&}nbsp;I do not understand what is happening!<<\_div_>>". (Sorry, I had to "fake" the HTML tags because the forum changes it to HTML!)
How do I get rid of the HTML tags but maintain the HTML/rich text formatting?
AH! I figured it out. Hope this is useful to others:
I had only set the textbox control on the form to Rich Text. I needed to also go into the target data table itself (tblSupeGenNotes) and set the [Note] field to have a "Text Format" property of "Rich Text" instead of "Plain Text".
After making that change, the rich text displays properly on the form, and, the text with formatting is correctly transferred into the target table.

VBA Assign a RecordSet Field to a ComboBox

I have the following code:
sSQL = "SELECT CODER FROM " & dbfname & " IN " & dir & " WHERE TRIM(CODEK) = TRIM(kCode)"
Combo29.RowSource = sSQL
Combo29.Requery
, where "CODER" is a field in the dbf file. "CODEK" is also a field in that dbf file, which im comparing with the string kCode.
When I run the code and when I click on the combobox, it asks me to enter arguments instead of showing the selected arguments. The RowSource type is set to Table/Query.
Is the assigning statement incorrect and how can I modify it to show me list of results from the SQL statement?
If I understand your problem correctly kCode is a string in VBA so you'll have to set up your SQL string the following way
sSQL = "SELECT CODER FROM " & dbfname & " IN " & dir & " WHERE TRIM(CODEK) = TRIM('" & kCode & "')"`