How to Get the Column/Field Name Using Access VBA - ms-access

I've copied the recordsource of a subform to a recordset by RecordClone. this subform source is composed by a query (some tables) and all the fields of this query were renamed like, for instance, id_document to "ID Document", date to "Document Date". what I need is to get those new names instead of the original names. instead of id_document, I want to get "ID Document". I tried to get them in the property or fiels collection, unsuccessfully. if any of you know how to do that, please, help me. I'd appreciate your help.
thanks in advance.
P.S.: this is the way I renamed the field in the query design view:
"ID Document": id_document

Take a look at this excellent answer from Gord Thompson (whose name is familiar to many here):
How to export a table in access to CSV with dot in field names through VBA?
It's not exactly what you're doing (this person wanted to remove a "." from field names) but you're doing something pretty similar. You can probably do a DLookup from a crosswalk table to figure out what the column should be named.
Just build a new table called tblCrosswalk and then fill it like this:
Old New
id_document ID Document
date Document Date
etc... etc...
Then, in the build the CSV header line section, instead of those "Replace" functions, put this:
s = DLookup("New", "tblCrosswalk", "Old = '" & fld.Name & "'")

Related

Expression in Textbox not passing value to the table

Obviously I am not an Access expert or I would not be asking this question. I created a small database with several tables. On a form, there are several combo boxes for the user to choose different combinations of medium, paper, sizes, etc. I have already created an expression that returns the correct value I need, but I cannot figure out how to get this value into the correct field on the table to store with the record the form is creating. Below are screen shots of the form and a couple of the tables. I have also included the expression I am using. I need the value that the expression returns to go into tbl1Artwork and populate the ArtWorkSKU field.
Expression:
=Left([PieceName],4) & [cbxArtist].Column & [cbxMedium].Column & [cbxPaperType].Column & [cbxPrintType].Column & [cbxSize].Column
The ArtWorkSKU text box is unbound as I had to type the expression in there. I am not sure if this is the correct way to accomplish the goal. In the tables below, except for the PK, all fields are Short Text.
All guidance is greatly appreciated.
Saving calculated data is usually not necessary and can be risky. Saved value can become 'out of sync' with raw data. Value can be calculated when needed.
Saving calculated data requires code (macro or VBA).
Can be as simple as Me!fieldname = Me.controlname.
Real trick is figuring out what event(s) to put code into. In your case, most likely form BeforeUpdate.
Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. Better would be ArtworkSKU or ArtworkSKUnum or Artwork_SKU_Num.
Create the following function in VBA:
Function UpdateSKU()
Me.ArtWorkSKU = Left(Me.[PieceName],4) & Me.[cbxArtist].Column(3) & _
Me.[cbxMedium].Column(2) & Me.[cbxPaperType].Column(2) & _
Me.[cbxPrintType].Column(2) & Me.[cbxSize].Column(2)
End Function
Then, on the form, update the After Update event property (not vba) of each "feeder" control to:
After Update: =UpdateSKU()

MS Access - Dlookup criteria from selected item (listbox)

I've seen similar questions but the provided answers couldn't solve my problem.
In access I created a form.
From a listbox you can select a name. The names are listed in the table tNames in the column names_combined (last name, given name) . In two other columns last name and given name are separated.
On the right side of the listbox you can find information about the name which will be shown in text boxes.
The goal is to show the last name from table tNames.lastname by looking for tNames.names_combined.
So I tried this:
=Dlookup("lastname";"tNames";"names_compined =" & Me.listbox)
However I just get error messages in my text box.
Thanks in advance!
DLookup requires commas not colons.
This should work assuming all the names of tables/fileds and controls are correct:
=Dlookup("lastname","tNames","names_compined='" & Me.listbox & "'")
Also make sure the actual bound field of your listbox is the combined name (by the way you code says comPined).
Finally as it was pointed out in the other answer me.something will only work in the form itself or its VBA module. Everywhere else you need a global identifier.
Try with:
=Dlookup("lastname";"tNames";"names_combined = '" & Forms!YourForm!listbox & "'")

Access 2010 VBA Form Pulling In Table Data

I have an "Invoices" table, which I run an "Invoices From off of to create invoices for my clients. All my client data; address, hourly rate, etc. is on my "Client Lists" table. The Tables are linked together by a common account/client ID.
I don't want to update general items like hourly rate, address, every time I create an invoice. I am looking to set up an after update event that will auto populate these general items from my "Client Lists" table when I enter the client ID on the "Invoices" form.
---Update---
Below is the code I am working with, and I keep getting an error "Expected: list separator or).
In normal speak; The service type field on my "Invoices" form, should look at my Service type field, on my Client Lists table, for the same Client ID as Account ID.
Private Sub Client_ID_Change()
Service_Type = DLookup("Service_Type", "Client Lists", "Account ID=" & Client ID)
End Sub
Have you tried anything?
The short answer is:
create a query to represent the data you want. In vba use a recordset to run the query's logic for the ID you've got, then display the returned data in the recordset on your form.
However there's no detail here, as you have little yourself
Syntax error on the dlookup function.
To specify a search criteria (last parameter of dlookup) based on a field that contains a blank space, you will have to enclose it in brackets. Also consider that control names can have blank spaces, but to reference them in vba you must replace write them using underscores.
"[Account ID]=" & Client_ID
I would strongly suggest you name table fields and form controls without blank spaces.

Filtering A Lookup Field Based On Another Field

I have a lookup field in my table based on another table. I'm having trouble filtering those values based on another field that is entered prior to the field.
Is it possible to filter a lookup field based on another field?
EDIT
Let me try and clarify my original question, sorry about that. Ok, so I have a table1 that has the following fields: ID, Name, Logo.
If a user enters a specific name in the Name field, when they click on the Logo field, it'll only display those values associated that are similar to the name entered. Does that make any sense? If it does make sense, would there be an easier suggesion on accomplishing this task?
If you're talking about inside a table, the answer is "No". You can create cascading combo boxes on a form, but you can't base a lookup value in a field of a table off of a different field in that table (or the field in any other table).
Here is an example of how to handle filtering a combo box based on the value selected in another combo box:
I have the following form:
The combo boxes are named cboIntPN and cboManPN.
The Row Source for cboIntPN is set to: SELECT uniq_key, part_no, revision FROM inventor. The Row Source for cboManPN isn't set to anything.
When the user selects a value for Internal PN the following AfterUpdate Event is triggered:
Private Sub cboInternalPN_AfterUpdate()
[cboManPN].RowSourceType = "Table/Query"
[cboManPN].RowSource = "SELECT uniqmfgrhd, mfgr_pt_no FROM invtmfhd " & _
"WHERE uniq_key = '" & cboIntPN.value & "'"
End Sub
It sounds like he is having the same issue as me. I also wanted to filter a field in a table for data entry on another field's input and my conclusion is "it is time I stopped entering data manually in tables and begin to create Data entry forms. I was putting this task off until later, but if I don't do it now, I might make worse trouble for myself later.
Btw, what an old thread.

Returning specific field from a matching record

I'm still a newbie at Access 2007, but I feel I am missing a understanding of a concept, when it comes to using user input from an unbound text box.
I'm trying to have the user input the record number (i.e. A12) and return another field in the matching record (such as the record status like "Opened")
I'm fiddling with DLookup to see if it will work through that method but no luck yet.
I may look into SELECT - SQL, but I haven't used that function yet and not sure if that will give me the result I'm looking for.
If this is something elementary to access programming (or databases in general), please let me know where I can read up on this.
I am currently using the MSDN website, but examples go much further to play with.
Edit:
My DLookup so far, which happens after update from user on Text12
Me.Text14.Value = DLookup("[RecordStatus]", "Orders", Text12.Value)
Thanks
Look closer at the third option (Criteria) in your DLookup() expression. You gave it only Text12.Value, which I assume is a string value like "A12".
The Criteria parameter should be like a WHERE clause in a query, without the word WHERE. IOW, some field's value = "A12". If that field is named "record_id", try this:
DLookup("RecordStatus", "Orders", "record_id = '" & Me.Text12 & "'")