I am storing postal addresses in an access database. How can I make it so that it is stored in the correct format so when selected using a query it will all be on separate lines?
Are you talking about a TextBox? If yes, just set the TextBox's EnterKeyBehavior property to True in code or set Enter Key Behavior to New Line in Field in the properties window (section "Other").
===
If you want to concatenate different fields in a query as a multiline string, do something like this
SELECT
[Name] & Chr$(13) & Chr$(10) &
[Street] & Chr$(13) & Chr$(10) &
[City] & ' ' & [ZipCode] As AdrMultiline
FROM
tblAddress
pressing ctrl+enter will insert a line break for a textfield. that way all data is stored in one field, but the text will be broken up into multiple lines.
I suppose you could put a field in the data for line number, then in the query sort on that field but do not display it. Your source data would have a separate record for each line of the address.
Related
I have a table called A5K with a few columns, Most important being Location and Serial number
I have a split form with a multi line textbox and a search button. If I type in any location or serial number and click search. The record would appear in the datasheet form below.
What I'm trying to do is to search for multiple serial numbers/location at a time and these would appear in the datasheet.
For example, lets say I'm searching for location A,B,C&D, I would like to enter these into the multiline textbox as
A
B
C
D
Click search
and records A,B,C&D would all appear.
It can be done but is poor design. VBA code would have to build comma separated parameter array for use with IN() function. If the field type is text, also need apostrophe delimiters. Consistency of structure is critical in string manipulation. If the items are ALWAYS typed with a single CrLf between them:
Me.Filter = "Location IN('" & Replace(Me.textboxname, vbCrLf, "','") & "')"
Me.FilterOn = True
I have made a form that is used as an inter-department sign-off sheet for controlled documents on the network. The database already includes a table with a hyperlink field for each document's location. The table also has a text field of the document id. My form is storing its information in a different table, but I am hoping that I can still use the hyperlinks from the first table instead of creating new links in the second table.
My google searching lead me to this forum post that showcased a single line using FollowHyperlink and DLookup:
Application.FollowHyperlink DLookup("Document_ID", "Documents", "Document_ID = '" & Me.DocumentID.Value & "'")
Document_ID is the field that holds the hyperlink.
Me.DocumentID is the textbox that the user types in and the code runs when this field is clicked.
I have tried multiple variations of the code including wrapping table fields in brackets "[]", using either the hyperlink field or the text field for the criteria, using Like instead of = (along with asterisks around Me.DocumentID). All of which result in a run-time error 2471:
The expression you entered as a query parameter produced this error:
'Document_ID'
Which makes me think that Dlookup doesn't like hyperlink fields as I can pull other fields just fine. What am I missing? Or is there a better way to reference hyperlinks in a different table?
Thanks to HansUp for pointing me in the right direction. Turns out that "Document_ID" didn't exist in the table. Instead the hyperlink field had a space between the underscore and ID ("Document_ ID") and I didn't catch it until copying the name from the table into vba where spaces are more pronounced.
The Dlookup would not work when using the hyperlink field in the criteria so the text field with the document id was used instead and changed my code to the following as .FollowHyperlink wouldn't take the whole field value (title#address)
Dim LinkText As String
LinkText = DLookup("[Document_ ID]", "Documents", "[DocID] = '" & Me.DocumentID & "'")
LinkText = Application.HyperlinkPart(LinkText, acAddress)
Application.FollowHyperlink LinkText
The control will now open the documents that exist (now to work on code to use for when the documents don't exist)
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] & "'")
I am trying to build an update query in which once the user enters some text into a text box I have installed on a form, will then have a query (or perhaps loop) run so that all records in an underlying table which contain the value entered into the text box will be flagged.
At the moment my strategy is to take the value in the text box, paste it into a holding table (which I called tblSearchEngine07), and then run an my query which is aimed to identify all records where in the field called 'tblMasterListOfEventsNotes' the value pasted in my holding table will be referenced. Unfortunately, I am having a syntax issue and am not successfully getting the holding table value to be correctly inserted into my query.
This is my current query syntax:
SELECT tblSearchEngine01.tblMasterListOfEventsNotes
FROM tblSearchEngine01, tblSearchEngine07
WHERE (((tblSearchEngine01.tblMasterListOfEventsNotes) Like "*[tblsearchengine07].[parolachiave]*"));
Try to substitute your like with this:
like "*" & tblsearchengine07.parolachiave & "*"
Update.
If you are not interested in storing your search values in a table, you can simply create your query string by reading textBox.value like this:
Dim sql as string
sql = "update yourtablename set yourfiled = true where yourconditionfield like '*" & textboxname.value & "*'"
Docmd.runsql sql
I want to create a query which takes field parameters through a form. For this, I created a form with combo boxes and drop down options to select the values from, this populates a text value in the respective invisible text fields whose default value I have set to null. Now in my query I give criteria for column as iif(isNull([Forms]![Conditions]![text_on_form]), [column_in_table], [Forms]![Conditions]![text_on_form]). I have done this for all the columns on which the where clause comes from the form. I have tried running this. The results seem to be random. It worked for three columns, but when I played around with it, it was giving me empty result set. Can anyone tell me what I am doing wrong? Or if there is a better way to implement query by form in Access.
It sounds like you are trying to create dynamic SQL. Here is the method in vba I generally prefer:
Dim SQL As String
SQL = "SELECT tblName.* From tblName WHERE (1=1)"
If Not IsNull(Me.combo1) Then
SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
End If
If Not IsNull(Me.combo2) Then
SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
End If
Docmd.RunSQL SQL
End Sub
Basically, add on to the SQL statement only if the user has put a value into your text box/ combo box or whatever. The "Where (1=1)" is to account for a situation where all fields are null.
Play with this concept to create your SQL statements. Avoid using invisible text boxes to store data, it generally means you are doing something wrong and will get mixed results (someone else on this forum can explain better than me why that is).
Just use the Like operator. Put this in your criteria field in the query Like "\*" & Forms![Form_Name]![Form_Field] & "\*" -- This tells it to get anything if the field is blank (or null) and matches whatever you have in the field. This may not be what you want. It should be noted that it will return anything with the text string in it. For example: if you type "the" it will return tether, these, theses, thermometer (anything with the word "the" in it. It works best for multi word or longer strings that can be matched more accurately, however it works for a search query because there is usually a set of human eyes looking for the result and erroneous results is not a huge problem.