NA in textbox following DLOOKUP - vba

I am using a dlookup to display some table content in a textbox but its returning #NA
For example: I have a table with 2 columns called USER1 & Total Calls Answered, and the table is called hourly1
My Dlookup is:
=DLookUp("[Total Calls Answered]","[Hourly1]","[userid] = [USER1]")
Why is my textbox showing #NA

As soon as the table [Hourly1] has only two fields, it means that it has no field [CDP1]. If this is the name of control of your form, use this for numeric CDP1:
=DLookUp("[Total Calls Answered]","[Hourly1]","[userid] = " & [CDP1])

Related

How can a combobox store different type field values in MS Access database or have a dynamic column type

My goal is that the user chooses the type of field search and according to that another combo box show that available field:
CustomerID, CustomerFirstName, CustomerLastName, Phone are the columns of the Customer table.
If I choose customer ID in first the adjacent box shows:
When I choose first name as an option:
But after pressing Enter on "Dog", I get an error
The value you entered isn't valid for this field
The code for the combo box 2 is:
Private Sub SelctionBY_AfterUpdate()
Dim iVal
iVal = Me.SelctionBY.Value
Dim S As String
S = "SELECT " & iVal & ", CustomerID, CustomerFirstName, CustomerLastName, Phone From Customer"
Me.Combo33.RowSource = S
End Sub
So how do i fix this?
A combobox doesn't store anything, it's just for selection of something!
Data is stored in table fields and a combobox can be bound to a field to store data.
Your issue is that the bound field type changes, but it shouldn't. You can avoid that by binding the data field to the second column of the combobox, asCustomerIDis customer tables PK(Primary Key) that should be stored as FK(Foreign Key)
Me.Combo33.BoundColumn = 2

Sorting Data on Form in Access 2010 by VBA

I have the following setup:
Table "Mitarbeiter" (Users) with fields: "UNummer" / "Sortierung" /....
Table "Mo01" (a sheet for every month) with fields: "UNummer" / "01" / "02" / ....
The Field UNummer in Table Mo01 is a combination field that gets Mitarbeiter.UNummer and saves it as text
I call a Form "Monatsblatt" that is based on the table Mo01.
In that Form I have a Field "fldSort" that is calling "Sortierung" from table "Mitarbeiter". The Data in that field is based on "=DomWert("Sortierung";"Mitarbeiter";"UNummer = '" & [ID] & "'")"
This works and looks like this:
I am trying to sort the form by that "fldSort" in Form "Monatsblatt" by using this code:
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
When I start the form with that code running, Access asks for parameters:
I tried a lot of different ways of writing the code, referencing to the field in different ways. I do NOT want to base the form on anything other then the table.
Why not ask the wide world watch "Why Access asking me for Parameter"? That would have brought you to the clue I think. Debug.Print or MsgBox your .OrderBy and you see it's "fldSort", not a valid sort. Access is assuming you want to use a parameter called fldSort, but you want the string in the variable fldSort, but it's not recognized, because of the double quotes surrounding it. Everything between 2 double quotes is interpreted as a string, even it's a var name.
Delete the quotes and everything will work fine (if your sort string is sufficent)!
Form_Monatsblatt.OrderBy = fldSort
[Update]
Late, but now I see the clue. You added a calculated field to the form, but you can't sort or filter them.
Instead of appending this field to the table, create a query and add it there, then you bind the form to the query and add the field to the form. Now you can filter and sort as you like!
The query looks like this:
SELECT *,
Dlookup("Sortierung","Mitarbeiter","UNummer = '" & [ID] & "'") AS ldSort
FROM Mo01;
Or with a join:
SELECT
Mo01.*,
Mitarbeiter.Sortierung AS fldSort
FROM
Mo01
LEFT JOIN
Mitarbeiter
ON
Mo01.ID = Mitarbeiter.UNummer;
Now you can use
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
because you have a bound control called fldSort.
[/Update]

Use an Access Forms Unbound text box as a Field filter in a table

Access 2013 - Reference an Unbound text box on a Form
I am currently trying to use an unbound text box [Text161] on a Form name [DCM_Gap_Servers] to sort information through a table. I want the query that I created to be able to take the users input from [DCM_Gap_Servers]![Text161] as the field that is being sorted from the table names 'Server'.
This is the SQL I am using right now in the query:
SELECT * FROM Servers WHERE "Forms![DCM_Gap_Servers]![Text161]" IS NULL
** I have already Tried:
"Forms![DCM_Gap_Servers]![Text161]" ; (Forms![DCM_Gap_Servers]![Text161]); Forms.[DCM_Gap_Servers]![Text161]
This will work at any time if I replace the Text Box reference with the actual Field name I am using, but since there are hundreds of combinations of fields, I need the reference to work.
I have looked all over, and I can't seem to find the correct answer. I am willing to do it in VBA if needed, whatever it takes to get the filtering done correctly.
Thank You.
It is:
SELECT * FROM Servers WHERE Forms.[DCM_Gap_Servers].[Text161] IS NULL
but that will just select all records whenever your textbox is Null.
So it rather is:
SELECT * FROM Servers WHERE SomeField = Forms.[DCM_Gap_Servers].[Text161]
To use the form value as a field name, you must use concatenated SQL:
strSQL = "SELECT * FROM Servers WHERE " & Forms![DCM_Gap_Servers]![Text161].Value & " IS NULL"
This you might pass to the SQL property of an existing query object:
MyQueryDef.SQL = strSQL
Or:
Constant SQL As String = "SELECT * FROM Servers WHERE {0} IS NULL"
FieldName = Forms![DCM_Gap_Servers]![Text161].Value
MyQueryDef.SQL = Replace(strSQL, "{0}", FieldName)
Of course, take care the the field name isn't a zero length string.

Vba code to show selected data from combobox

I have an Access 2010 form which has a combobox listing three columns:
ID
first name
last name
The combo box is bound to a table containing this data. The ID column in the combo box is hidden, it only shows the first and last name.
When the user selects a row only the the first name is shown. in the property section, I chose:
Column Count: 3
Column widths:0;3,3
Bound Column: 1
I made another text field and in the combobox I wrote the following vbcode:
text=combo.value
that shows in the text field the chosen ID.
I want to show in another field (text\combo?) the last name.
How can I do that?
well I did it with recordset.
if someone know a simpler solution, I will be glad to lrn.
this is my code:
Dim dbs As ADODB.Connection
Dim id As String
Set dbs = CurrentProject.AccessConnection
Set rsRep = New ADODB.Recordset
Set rsRep.ActiveConnection = dbs
rsRep.Open "tblRep"
id = Me.cbPrvFirstName
rsRep.MoveFirst
rsRep.Find "RepId=" & id
Me.txtPrvLastName = rsRep.Fields(2)
rsRep.Close
You can pull in the value from another column within the combo box using the below code. By default combo.value will always give the value from the first column even if hidden.
try
text=combo.column(x)
in your case to retrieve last name it would be
text=combo.column(2)
where x is a NUMBER of the column you want to retrieve 0 being the first column
You could also try an alternative - This works like a VLookup in excel if you are familiar with that.
(Not as simple as the first option ;) )
text=Dlookup("[last name]","tblRep","RepId=" & combo.value)

Linking result of a query that related to a Listbox value to textboxes?

I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).