Placing variable number of variables into fields in table - sql

I am trying to make a generic notInList function that is called when the user types a value in a combobox that is not part of the list. In middle of that function, I want to insert the new value(s) into a table.
For some of the combo-boxes, more than one field has to be filled out in the table. (The user is asked a follow-up question about the not-in-list value, and the answer to the follow-up also has to be put into the table). The values that have to be inserted in the table are stored in variables in the code.
The way I have been dealing with this so far is through a table that has one record for each combo-box id, field, and variable name (the name of the variable that contains the value to be inserted into the field) combination. The code loops over all the records that relate to this combo-box and builds one list of field names and one list of variable names to be used in a SQL statement (Insert...values...).
However, I can't figure out how to use the name of the variable (retrieved from the table) to get the value stored in the variable. AddVar is the column in the recordset that contains the name of the variable I am trying to get the value of. I tried eval(rs!AddVar) but that doesn't work.
I am able to get the name of the variable from the table, but then I am stuck. How can I get the value (a string) stored in that variable?

You might be interested in the Dlookup() function. Basically, you just pass it a field name, a table name, and some optional criteria, and it will give you the first result it finds.
Consider the example:
=DLookUp("[LastName]", "Employees", "[EmployeeID] = 7")
This will go into the "Employees" table, find the first record where "[EmployeeID] = 7", and return to you the [LastName]. Hope this helps.
http://support.microsoft.com/kb/208786

Related

Should the datatype of lookup fields in Access be different from the source table?

I am trying to use VBA to populate a bunch of tables based on the values of fields in a main table. The main table has a field "Sample Name" which is linked to a lookup field in each of the tables I am trying to populate. In the main table the data type of Sample Name is dbText (10), however I'm noticing the data type of the lookup field based on Sample Name is dbLong (4). This causes problems for my code as I am trying to add new records to each table, and when I try to set the value of the lookup field to a corresponding value from Sample Name (stored in a string) I receive a data type conversion error.
Is there a reason the lookup field has a different data type than its source table? Is the lookup field storing some sort of index like the key from the main table and simply displaying the corresponding value from Sample Name?
Additional background:
The block of code (within a Case statement) that throws the error is as follows, at this point revlitho has been defined as a recordset, the "Sample Name" field is the lookup field in question, and sampName is the string variable storing the corresponding Sample Name from the main table. It is also worth noting that at time of error revlitho.fields("Sample Name").value returns Null, I am unsure if this is the default for empty fields in a recordset:
Case "Standard Image Reverse Lithography"
revlitho.AddNew
revlitho.Fields("Sample Name").Value = sampName
revlitho.Update

Conditional looping with ADO Recordset in VBA

In the past, I've always generated reports by querying a database, dropping it on a worksheet, sorting, reading into an array, and then looping through to create the documents. What I'm trying to do now is generate the reports directly from the recordset.
The problem I have though is when using arrays I had an absolute record number, so I could check if a field in the previous record was the same as in the current record.
What is the best way of going about doing this with a recordset? Store the field value in a variable? Use nested While loops? Get a total record count and use absolute positions to move back and forth (which doesn't seem like a good idea)?
UPDATE
The recordset contains multiple records from 20 different companies. I sort the query by the company name and then start loop through it. I then need to check each record to see if it's from the same company as the previous and if not create a new worksheet for that company.
I usually do the sort in the query using an "Order By" clause. Then just loop through the sorted records using a variable to track the current sort order name and detect the name change by comparing the current record name field to the variable value.

How to interact between two tables on both directions in Excel?

I have 2 sheets in my Excel project:
In "Sheet1", I have a big data table, with 4 columns:
DataTable[Country], DataTable[Brand], DataTable[Parameter], DataTable[Calculated].
On "Sheet 2" I have a dashboard. I have there a table called FilteredTable that presents the data from DataTable. I have there a drop down command that lets the user select country, and the table is filtered accordingly.
I want that the FilteredTable will not only show data from the original DataTable, but to let the user to change the [Parameter] column. When changing it, I want that the [Calculated] column in both tables will be updated accordingly. If the user will change the country, then the FilteredTable will show the parameter that is stored in DataTable for that combination of [Country] and [Brand]. And if the user will get back to the first country, the displayed parameter will be the last one that the user entered.
I am a bit confused how to do it.
What I have done so far is:
1) to read into FilteredTable rows from DataFilter, using a formula array. I am mirroing this way [country],[brand] and [calculated]
2) in the DataTable[paramter], I read from FilteredTable[parameter] the same way, with a formula array.
It works fine, untill I change a country, and then the parameters in FilteredTable are already do not match the new country, and in DataTable, the parameters for the old country where changed to 0.
I'm in a logical loop. Is there a way out?
Thanks
Have you considered using 3 tables instead of 2? This would be: the original data table, a table which pulls in all values created through manual entry from the display table, and the display table itself.
In VBA, create a function which runs each time a formula is changed in the display table. That function should take the new data from the user, and copy the altered row onto the 'manual entry' table. For the display table itself, you could either recalculate manually through VBA every time a change is made, or have a formula which looks to see whether there is data in the manual table. If there is data in the manual table, that is where the value is pulled from. Otherwise, data is pulled from the original data table.

Looking up values in a table with VBA

I am trying to lookup values in an excel table using VBA and having trouble getting it all together. I get most of the way there but my code is not worth posting.
What I need to do is write a function with several parameters:
1. What value to look up
2. What column name to look this up in
3. What other column to return the value from
For example, in a table containing Customer_ID, Last_Name, First_Name: pass it "DoeJohn", "Customer_ID", "First_Name" and have it return John. I am not concerned with duplicate values of what I am looking up, finding the first instance is good enough.
The cell it finds may be text, numeric, or date so it needs to return that.
If it doesn't find a match it should return something that could never be an actual cell value, and my main program will check for that.
If you are familiar with writing Functions() in vba you could simply have a custom function that contained a few vlookup() functions then organize the output and return it however you want.

Accessing a datatable via a string variable

This is probably one of those that is so simple I can't see it. I have a string variable called Market. The variable is user chosen and is the exact same name as one of many tables in my dataset. Basically I am having the user choose which table they want in a combobox, then I want to use that variable to access the table. So if the user picks "Market1" then I want to open the table named Market1.
I am simplifying here, but need to know how to open:
For ds.<variable here>.rows.count - 1
'perform steps
Next
How do I inject the variable correctly? Thanks ahead of time!
Dataset has Tables property, which accepts table name as a parameter. So in your case if string variable Market holds table name, you can access the table by referring to ds.Tables(Market)