Comparing field names of two tables - vba

I have spent a few hours searching for a similar Problem to mine. though there are a few replies, i could not find any to the Point.
so basically, i have two tables. one is a Standard Access table (table1) and the other is a table imported from Excel (table2). now table2 has the field names of table 1 and also a few other extra fields that i dont Need. now i want Access to compare These two tables and copy into table 1 the contents of whichever fields that it can match from table 2.
i have tried INFORMATION_SCHEMA.COLUMNS, but the code does not seem to recognise this Expression.
can anyone suggest some ideas please!!! can give more Details if required.
Thanks a lot..
saran

Something like this would give you the field names.. If static field names, id suggest importing to a table, derrived from your current import, and delete and append rather importing each time, and use a join as suggested.
Dim a As ADODB.Connection
Set a = New ADODB.Connection
Dim r As ADODB.Recordset
Set a = CurrentProject.Connection
Set r = a.OpenSchema(adSchemaColumns)
r.Filter = "[TABLE_NAME]='Your table name'"
or another way
r.open "Select * from table",a,1
loop r.fields

Related

Joining two tables listing all the results from the first table

I have two tables related to each other. The first table has one or more number of the same attribute on the latter table. (there is a one-to-many relationship) thus the rows on the latter table includes basically the following columns: (PK, FK, attribute). Some rows of the first table does not have an attribute and are not recorded in the latter table.
I would like to list all the rows in the first table with an extra column for the attribute from the latter table. when I use inner join or where clause I cannot list the results that have no attribute. However what I want to do is to list all the rows from the first table, and if there is no attribute for it I want to make the extra column null, and if it has more than one attribute I want to concatenate them in that single column.
I am aware that I am not sharing any code here and I am asking the question in an abstract way because first I need to know which tool to use and in what way. I tried joins and sub-queries with select statement as well as methods such as ISNULL but I was not sure what I was doing and it did not work for me.
PS: I am new to the site as you can observe. Thus if you think this is not a proper way of asking questions and if you suggest removing this question off the forum I will no question asked.
SELECT TableA.*, TableB.Attribute
FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.FK
Edit: I just read the original question more fully and realised that this is not quite what you're after. This will give you the NULL value you wanted if there is no related data in TableB, however you will need to write a function if you want multiple related values in TableB concatenated into the one row for a given TableA record. Create a new module and in here add the following function. I am assuming here that your PK and FK columns are Long Integer and the Attribute values are String. I am also assuming that you're working in MS-Access.
Public Function ListAttributes(lngPK As Long) As String
Dim rsAttributes As DAO.Recordset
Dim strResult as String
Set rsAttributes = CurrentDB.OpenRecordset("SELECT Attribute FROM TableB WHERE FK = " & lngPK)
Do While Not rsAttributes.EOF
strResult = strResult & ", " & rsAttributes!Attribute
rsAttributes.MoveNext
Loop
If strResult <> "" Then
strResult = Mid(strResult, 3)
End If
ListAttributes = strResult
Set rsAttributes = Nothing
End Function
Your query then becomes
SELECT *, ListAttributes(PK) FROM TableA
Note that you will now get an empty string instead of NULL for rows in TableA that have no corresponding rows in TableB, but you can always use an IIf function call to fix this if necessary.

An elongated copy query in MS Access

So, I learnt the joys(/s) of INNER JOIN today. Now I need to copy select records from certain specific columns into my main column. Once again, I'll be using the following format:
Please bear in mind that I'm working with 300,000 records. Copy/pasting one by one is not practical.
Tables:
MainTab
CritTab
Key column:
MainTab:- LINK
CritTab:- FORNLINK
Columns to be copied
CritTab.DATE
CritTab.CODE
Criteria
WHERE CritTab.[CODE] = <This kinda code> OR <This other code>
AND CritTab.[FORNLINK] = MainTab.[LINK]
So, to clarify: I need to copy specific columns from CritTab to MainTab. As not every record in MainTab will have a corresponding CritTab entry, I cannot simply copy and paste the entire column, as some records won't match up.
Is it possible to do this with a query in Access?
I'm not sure if you need the columns added or not, but if you do run these first. I am changing the column name because you should never use keywords as a column, table or variable name.
ALTER TABLE MainTab ADD COLUMN DATENAME DATETIME
ALTER TABLE MainTab ADD COLUMN CODENAME (WHATEVER DATA TYPE IT IS IN CritTab)
I am moving the JOIN operator from the where clause to the FROM clause. This usually makes it easier to understand how the data is being built and clutters the WHERE a bit less in my opinion. Note: I am doing the same sort of syntax as the article Mike posted in the comments.
UPDATE MainTab INNER JOIN CritTab ON CritTab.[FORNLINK] = MainTab.[LINK]
SET MainTab.DATENAME = CritTab.[Date], MainTab.CODENAME = CritTab.[CODE]
WHERE CritTab.[CODE] = <This kinda code> OR CritTab.[CODE] = <This other code>

SQL Query in Access to prompt with Message Box asking to change table name

Is there a way to be prompted before you a run an SQL query in Access, to enter in the table name that you wish to query? For example, lets say the columns will always stay constant. The columns could be called "Fruit" and "Date." But, the table name could change depending on the batch number. Ie. table name could be "BatchNO_1" or "BatchNO_2" or "BatchNO_3" etc. So Lets say i have an SQL like:
select Fruit, Date from BatchNO_1 where Fruit = "Apples"
Is there a way that I can be prompted to enter in the table name and have the SQL use the table name i enter to perform the query?
No. The table name cannot be passed as parameter to a query. You will have to construct the query yourself.
Dim tableName as String, sql As String
tableName = InputBox("Please enter the table name")
If tableName <> "" Then
sql = "SELECT Fruit, Date FROM [" & tableName & "] WHERE Fruit = 'Apples'"
'TODO: execute the query here
End If
For instance, you could change the query text of an existing query like this:
CurrentDb.QueryDefs("myQuery").SQL = sql
Or you could execute the query like this
Dim db As DAO.Database, rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(sql)
Do Until rs.EOF
Debug.Print rs!Fruit & " " & rs!Date
rs.MoveNext
Loop
rs.Close: Set rs = Nothing
db.Close: set db = Nothing
By putting the batch number in the table name instead of as a column, you are encoding data in the schema. This is not best practice, so in my opinion, the correct answer is to change your database design.
Make a single Batch table with all the columns from your current BatchNo tables, but add a column named BatchNo as part of the primary key. Load all the data from your BatchNo tables into this one, and then delete those tables. Then your query will straightforwardly look like this:
SELECT Fruit, Date
FROM Batch
WHERE
Fruit = "Apples"
AND BatchNo = [Enter Batch No];
Don't put data in table names. That is not the way databases are supposed to be made.
Just to explain a little bit, the reason that your current design violates best practice is due to exactly the problem you are facing now--the shenanigans and weird things you have to do to work with such a design and try to perform operations in a reasonable, data-driven, way.
By having the user enter the table name, you also create the danger of SQL injection if you aren't also careful to compare the user-provided table name to a whitelist of allowed table names. While this may not be such a big deal in Access, it is still heading down the wrong path and is training for something else besides professional database work. If you would ever like to grow your career, it would be regrettable to first have to unlearn a bunch of stuff before you could even start with a "clean slate" to learn the right way to do things.

I want to go to last record of access database without using ID

I have been trying to find this. I am using access 2010 and I have some data in a few tables and I want to select the last row from each one and add them to a new database. All the databases have random ID so I can't use the Sort by ID function.
If the table is small, you can pass it to a datatable in the frontend and use something like this,
lastRow = datatable.rows(datatable.rows.count-1)
Else, You can add a 'created_datetime' field in database which holds the inserted datetime and retrieve its maximum date since your ID field has random number...
open vba (alt + F11)
dim rst as recordset
set rst = docmd.runsql (sql statement here) e.g. (select * from tablename)
rst.movelast
you've gotten to your last record in vba
you can add it to a new table database by using an insert statement.

Using a query to loop through tables that are similar in structure but have different names

I would like to use a query to loop through tables that are similar in structure but have different names (ie. tableJan2011, tableFeb2011, tableMar2011 etc.)
Is there a way in MS Access and in SQL Server to use the same query statement while varying the table name within it. (similar to using parameter values) (need this to add different input to each different month's table)
This is a bad table design. You should have a singe table, where you have a column(s) to indicate month/year. You would then just query this single table and add a WHERE month='X' and YEAR='Y' to limit your results to what you need.
without a table redesign use UNION and clever WHERE clause parameters, which will cause rows to only come from the table that applies.
SELECT
..
FROM tableJan2011
where...
UNION
SELECT
..
FROM tableFeb2011
where...
UNION
SELECT
..
FROM tableMar2011
where...
First off, listen to the people who are telling you to use one table. They know of which they speak.
If you can't do that for some obscure reason (such as inheriting the design & not being allowed to change it), then you're stuck writing VBA code. There's no way that I know of, in Access, to substitute source tables (or even source columns--values only), in a saved QueryDef.
You'll need something like this:
Private Function QueryTable (strTableName as String) As DAO.Recordset
Const theQuery as String = "SELECT tbl.* FROM [table] As tbl"
Dim sSql As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
sSql = Replace(theQuery, "[table]", strTableName)
Set db = CurrentDb()
Set rs = db.OpenRecordset(sSql)
Set QueryTable = rs
End Function
Note that this is simplified code. There's no error handling, I haven't released the objects (which I usually do, even though they'll go out of scope), and SELECT * is almost always a bad idea.
You'd then call this function wherever you need it, passing in the name of the table.
consider moving the year and month out of the table name and into columns in one table.
you can create a table with query or table names to use at runtime, but you have to be able to write Access BASIC code in a module.
Here's an example, assuming you have a query built on a table with the query names you want to execute:
Set db = CurrentDb
Set rsPTAppend = db.OpenRecordset("qry_PTAppend")
rsPTAppend.MoveFirst
Do Until rsPTAppend.EOF
qryPT = rsPTAppend("PT")
Set qdef = db.QueryDefs(qryPT)
sqlOld = qdef.sql
sqlNew = sqlOld
' manipulate sql
If sqlNew <> sqlOld Then
qdef.sql = sqlNew
End If
db.QueryDefs(rsPTAppend("append")).Execute
If sqlNew <> sqlOld Then
qdef.sql = sqlOld
End If
rsPTAppend.MoveNext
Loop
Don't know what is possible in Access but in SQL Server you could create a view that use union to get all tables together and then build your queries against the view.
One other option you have could be to build your queries dynamically.
In sql server you can execute a string as sql.
http://msdn.microsoft.com/en-us/library/ms175170.aspx
I'm not aware of anything similar in MS Access (though my experience is limited). You could however dynamically generate your sql in code to accomplish this. Perhaps you could create a function to take the table suffix and parameters and build the desired sql that way.