Access Form: Invalid ControlSource - ms-access

In my Access Form I have a Text Box that should have this value:
SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000"
So, I wrote in the ControlSource of this textbox the following string:
= ( SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000" )
but the result that I get is that the textbox.value is #Name?. Maybe this string should not be in the ControlSource?

Use DLookup:
=DLookup("[Company]"), "[QueryOne]", "[ID] = '1000'")
or more probably ID isn't a text column, then
=DLookup("[Company]"), "[QueryOne]", "[ID] = 1000")

Access text boxes cannot be directly used to execute SQL. You can however call a function from the text box by setting it as the Control Source and display the result. Access offers a number of functions which can be used to retrieve data from a database. DlookUp should do what you require here:
=DLookUp("[Company]","[QueryOne]","ID='1000'")
You probably want to be able to enter the company ID to return the name. To do that, change the function to refer to another input textbox on your form like so (obviously just change the form and text box names to match your elements):
=DLookUp("[Company]","[QueryOne]","[ID]='" & [Forms]![Form1].[txtCompanyID].[Value] & "'")
Of course, if you are doing anything more than the most trivial of lookups, the correct way of doing this would be to set up a Record Source for the form and set the text box Control Source to a field from this dataset.

Related

Obtain Value from MS Access Checkbox

I have an Access database in which I store some data, and that database has 13 tables plus a reference one.
I want to make a form where there are several checkboxes and a button. Each chekbox represents a table, and every table selected will be joined inside a query writen in VBA, associated with the button click.
I've already made the same thing in Excel, and it works perfectly, so the only problem here is that I don't know how to access the checkbox value and use an IF condition to get the correct SQL string.
To make it clear, here I have a IF statement for one of the checkboxes in Excel:
If Range("B8").Value = True Then
CTODStrc = ", CTODTYPE, CTOD.TEMPERATURE, VALIDITY, DELTAR, DELTAL"
CTODStr = " JOIN CTOD ON REF.ID = CTOD.REF_ID"
JoinStr = JoinStr & CTODStr
Columns = Columns & CTODStrc
End If
SQLStr = RefStr & JoinStr 'Query sentence
The SQLStr is the query text, and it has a prior "select" string which is added.
In Excel, the cell B8 was associated with the checkbox, but in Access I have to make this condition using a checkbox thats in the form - how can I do it?
I've tried Me.CbName.Value, but it says the command is not supported.
Thank you.
The checked state of a checkbox is given by the Value property of the checkbox control. This property may be 0 (unchecked), -1 (checked), or Null for a block-filled triple state checkbox.
Since the Value property is the default property for a checkbox, and assuming you are not using a triple state checkbox, you should be able to use simply:
If CBName Then
' Do stuff
End If

Use a text box as a wildcard search on a field within a linked SQL Server Table within MS Access

I've managed to successfully build a combo-box that will allow a user to select a record from a list to get an ID from a table. With the table I'm currently working with running to around 60,000 records, it's not realistic to use this method to find the record.
What I want the user to be able to do is enter a name in a text box, and a combo box be populated with the relevant records from the table where one of the fields matches that. So if the user entered 'This' into the text box, the combo box would present records where the field had 'This', 'This and That' and 'this'. It would not present the record that only had 'That' in the field.
Lets say the Text box is called 'txtBox', the combo-box is called 'comBox' and the field in the linked SQL Server table 'LinkedTable' is called 'SearchField'
I would suggest you not use the combo box. Just have a text box in which the user can type in a few characters and hit enter key.
You THEN display a “list” of results that allows the user to select and click on any of the results to “edit” or “view” the given row of data.
In the following screen shot we working with a VERY small table of 500,000 rows. We looking for smith, so we just type in smi and hit enter. The results are displayed instant, and at that point the user can type in “first name” or just a few chars of first name and further drill down and filter.
The form looks like this:
The code in the after update event of the text box is simply:
Dim strSQL as String
strSQL = "select * from tblCustomers where LastName like '" & me.TextSearch & "*’"
me.RecordSource = strSQL
So very little code is required. You could I suppose fill in the results to a combo box, but then the user has to type into a box, then select something from a combo box and then somehow you bring up that record for editing. That’s like 3+ steps for the user.
Just use what Google or most accounting or darn near any computer software does:
A simple text box – you type in a few characters and when they hit enter the results are displayed for the user to pick.
Note in above the user can click on the “glasses” icon button to launch a form that displays the single record. The code behind that button is:
Docmd.Openform "frmEditDetails",,,"ID = " & ME!ID
I went down a different route to the suggested option in the other answer. This met my needs more accurately and might help someone with a similar issue.
First, I amended the query on the Combo box so that it used the value in the text box as a criteria for one of the fields.
In the Criteria for the 'SearchField' within 'LinkedTable' I entered:
Like "*" & [forms]![*FormName*]![txtBox] & "*"
This will restrict the results in comBox to those where the SearchField contains the value entered into txtBox. This doesn't update dynamically however and will only update after the first value entered into txtBox.
To get around this problem, I added the following to the 'On Get Focus' event for comBox
Private Sub comBox_GotFocus()
Dim ctlCombo As Control
' Return Control object pointing to a combo box.
Set ctlCombo = Forms!FormName!comBox
' Requery source of data for list box.
ctlCombo.Requery
End Sub
This will force the combo box to run the query again, using the current value in txtBox, each time it is 'clicked on'
As a result, I get the restricted list of values in the combo box that I need and allows the user to 'search' for a record that can then be used for creating a new record on a form.

Best practice for working with long texts in ms-access

I would be very thankful if somebody resolves my problem.
I'm new in working with Ms Access and I still gain experience on its basic functionality.
I have a table MyItems. 2 of its fields are: ItemCode and ItemName. ItemName is a very long text (Memo type). I have also a query and a form with many fields. The form's record source also consists of many fields. All these things (associated with 1 field) have the same or similar names so I can't differentiate them quite well.
What I want is when I set the value of ItemCode (in a not bound Combobox or Listbox with name ItemCode) the value of ItemName to be displayed in a control - maybe TextBox.
I can display its value in a ListBox (by sql query in its row source), I have no problems with this, I have no problems with managing events, but the text is very long and is cut. I understood that unfortunately ListBoxes don't have multiline property. So maybe the most appropriate control to deal with is a TextBox. And maybe the most appropriate way to display the value is using DLookUp function in the TextBox's control source. But in this sea of items with similar or the same names I just can't deal with its syntax, I was trying again and again for a very long time. So I have 2 questions:
Are the TextBox control and DLookUp function in its control source the best way to extract long texts from a table without binding or there are more suitable controls (which directly work with sql query)?
What is the right syntax of DLookUp? - where exactly are there ' ', " ", [ ], .Value, =, &, where must I write the path to the table or the form and where it would be mistake? If I just write [ItemCode] what it would be associated with - the form record source, the table, the form control or anything else? I would be grateful if someone writes the correct syntax for my case or if he shares a link with plenty of examples for using DLookUp. Those that I found didn't satisfy me.
Either a bound control, or an unbound one. If unbound, you need to load the text with VBA code or with DLookup in the control source. There are no other options.
Personally I'd rather use the AfterUpdate event of ItemCode, and call DLookup there, but that's a matter of preference.
2.
It's not that complicated. You basically have the SELECT, FROM, WHERE parts of an SQL query in the 3 arguments. [] are needed for all identifiers containing spaces or other special characters, and when refering to form controls.
=DLookup("ItemName", "[my Table]", "ItemCode = '" & [ItemCode] & "'")
The single quotes '' are needed if ItemCode is text, not when it is a number.
You could also use doubled (escaped) double quotes, but that is much less readable.
=DLookup("ItemName", "[my Table]", "ItemCode = """ & [ItemCode] & """")
Now where does [ItemCode] come from?
Access first looks for a control on the form with the name ItemCode.
If there isn't one, it looks for a field ItemCode in the form's RecordSource.
These are the only ways [ItemCode] can be evaluated. To avoid confusion, it is recommended to name bound controls with the same name as their source field.
The syntax above is only valid if everything is on the same form. If [ItemCode] is on a different form, or you refer to it from a query, you use
=DLookup("ItemName", "[my Table]", "ItemCode = '" & Forms![my Form]![ItemCode] & "'")
For more complicated cases with subforms, see Refer to Form and Subform properties and controls
And to use it in VBA (in ItemCode_AfterUpdate):
Me!ItemName = DLookup("ItemName", "[my Table]", "ItemCode = '" & Me![ItemCode] & "'")

Subform field hyperlinks to open record in another form

I'm trying to replicate a feature in the Access 2007 "Issues" template/example database. When you open up the 'Issues List' form, and click on an ID, it behaves like a hyperlink and opens up that record in another form.
How can I replicate this? I'm not a big fan of using the Access 'create macro' feature and would prefer to use the VBA editor if possible.
Thanks in advance for your help.
I didn't examine that template database, but your description sounds like something you can handle with DoCmd.OpenForm.
Say your form includes a text box named txtID which is bound to a numeric field, ID, in the form's record source. Create a VBA procedure for the text box's click event to pass the current ID value as the OpenForm WhereCondition parameter. (This assumes the record source of the next form also includes that numeric ID field.)
Private Sub txtID_Click()
Const cstrForm As String = "YourNextFormName" ' <-- change this
DoCmd.OpenForm cstrForm, WhereCondition:="[ID]=" & Me.txtID
End Sub
If the data type of the ID field is text rather than numeric, include quotes around the value in the WhereCondition.
WhereCondition:="[ID]='" & Me.txtID & "'"
What you want to do is first set the text field you're interested to a hyperlink. You do this by setting Display As Hyperlink in the Format tab of the Property Sheet or you can do this via vba as following (although it seems to be bugged visually i.e. clickable link but not displayed as such)
myTextBox.DisplayAsHyperlink = acDisplayAsHyperlinkOnScreenOnly
After you do that, create a Click event. In the subroutine you can run the code to open your form filtered with the record number (or appropriate argument)
DoCmd.OpenForm "myFormName", acNormal, , "[ID]=" & Nz([Id], 0)

MSAccess - populate text box with value from query

I have a combo box and several text boxes on a form. When I select a value from the combo box I want it to run a query based on that value and populate the text boxes with the data returned by the query. The query should only return one record and the textboxes correnspond to different columns in that record.
I have this code:
Private Sub cbo_equip_loc_Change()
Dim location As String
Me.cbo_equip_loc.SetFocus
location = DLookup("NAME", "Query1", "position = '" & Me.cbo_equip_loc.SelText & "'")
Me.Text51.SetFocus
Me.Text51.Text = location
End Sub
But I get this error: "This property is read-only and can't be set"
Any ideas?
Solved: Im an idiot.
I had some value in the Control Source from something I was trying to do before. Removed that and it worked!
There is no need to do this:
Me.Text51.SetFocus
Me.Text51.Text = location
it is true that the text property is only available when the control has the focus, but the value property is available without any focus, or Access VBA is quite happy with just the name of the control:
Me.Text51.Value = location
Or
Me.Text51 = location
Textbox Text51 is locked, set property Locked to False.