adding records to a table using VBA - vba

I send out a newsletter each month and would like to record the contact id, date sent in a separate table. This table will record a history of all the newsletter sent. What is the best way to do this...Append to table or just create a do loop and add new records?

This should be pretty easy. Now, you didn't say is you are appending records into Access, or SQL Server, or something else. The example below assumes you are using Access, but you can easily modify the code just slightly to insert into any kind of structured database.
Dim dbs As DAO.Database
Set dbs = OpenDatabase("Full path to your db")
'You can then "execute" a SQL statement:
dbs.Execute "INSERT INTO Employees(Name, Number) VALUES('" & Worksheets("Sheet1").Range("A2").Value & "','" & Worksheets("Sheet1").Range("A3") & "')"

Related

Access VBA Putting name of a Field as a Record

I am with a little bit of a coding problem that I don't know how to solve. I want to put in the table as a record a field name of another table in the same Database.
I will give you an example:
Table1
What I have
Table 2
What I want
The table 1 is update weekly from an external source so I need to record the field name as a record in the second table using VBA language. Does anybody knows if it's possible?
Thank You in advance
You could do something like this
Dim db As Database
Dim fld As Field
Dim sql As String
Set db = CurrentDb
For Each fld In db.TableDefs("YourTable").Fields
sql = "Insert into YourSummaryTable([Date], Hours) select '" & fld.Name & "', sum([" & fld.Name & "]) as s from YourTable"
DoCmd.RunSQL sql
Next fld
Note that you've used a reserved word Date as a fieldname, which isn't best practice and requires the use of the square brackets in the query.

Excel not selected the range specified in query

i am bulk inserting the worksheet to a temp table in SQL, after that i am calling a stored procedure to move data from temp to main table.
for insert i am using the below statement
s = "select * into [" & ThisWorkbook.Sheets("Master Control").Range("F2") & "].[" & Environ("username") & "] FROM [ABC$A13:IU5000] "
cn.Execute s
the problem is that, even after putting the range in the query, it is only picking upto the last column which have data, which is creating problem.
How can i make it to select exactly the range i specified?
I just posted my code to Transfer an Excel Range INTO a Database
This works for me.
SELECT * INTO [TagetTable] FROM [Excel 8.0;HDR=YES;DATABASE=C:\stackoverflow\test-stub.xlsx].[ABC$A13:IU5000]
This is your Query string:
select * into [F2].[best buy] FROM [ABC$A13:IU5000]
Is F2 the name of the database your are trying to query? Your missing the filepath in the FROM Clause

Saving a query into a Table

I have searched for a question that I am having, though I find parts of the answer I am not able to understand it fully. I am using access2010.
In simple Terms, I want to filter a table [newsearch] to show the results based on my WHERE condition.
I can use SELECT and a WHERE condition and I get a result through a query, but I want this result to be saved into [newsearch], that means I want this [newsearch] to contain only the results of this query.
I tried using SELECT INTO but since my source and destination are [newsearch], it does not work.
the query I run now is:
strSQL = "SELECT * FROM [newsearch] WHERE [newsearch].[" & Me.Combo17 & "] = '" & Me.Text18 & "'"
Set qdef = db.CreateQueryDef("User query results", strSQL)
qdef.close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "User query results", acViewNormal
This gives me the result in a query table but I want it saved to [newsearch].
How can I do it?
select into query creates a table, but obviously you cannot create a table that already exists. You have 2 options:
Instead of selecting what you need, delete those records from your table that you do not need: delete from [newsearch] where field3<>xyz
You can use select into to create a new table, then drop [newsearch] and then rename the new table to [newsearch].

Adding ' at the start of each row of a table through sql

I have a column called "product-code". These are all populated. I am wanting to do a query that will insert a ' at the start of each field and then another query to add a ' at the end of the field.
So for example at the moment a product code might be fmx-2, after the query I would want it to look like 'fmx-22'
I am looking to do this for all the data sets within the table. I am using Microsoft Access
Thanks
In Microsoft Access you can use & char to concatenate string, and your query could be something similar:
update my_table set product_code = "'" & product_code & "'";

Automatically updating/duplicating a table in Microsoft Access with VBA

Back again! So I am currently trying to programmatically have a table (Table2) update after a button is pressed. I would like for Table2 to resemble exactly another table (Table1). I'm going through this effort because Access does not allow double relationships and I have need to create one. I will therefore have two tables containing all the contact info.
I tried deleting Table2 and then creating another copy of Table1 and saving it as Table2. This would have worked is Microsoft Access didn’t throw an error because I am deleting a Table that has established relationships. So I then tried to programmatically delete and then create the appropriate relationships. However, this turns out to be a tedious exercise. A little too tedious for my taste.
My next thought is to create an append query that automatically looks for differences between the two tables and updates Table2 accordingly. The problem is that I have no idea how to structure the SQL statement for such an append query. Also is there an easier way to do this using VBA that I am missing? Thanks in advance for your help!
There is no need to go to all this trouble. You can have double relationships in MS Access. Simply add the table as many times as you need it to the relationship design window, you will get Table1, Table1_1, Table1_2 and so on, but they are all just aliases for Table1. You can now add self-joins and as many relationships as you need.
Like so:
*People*
PersonID *People_1*
ManagerID -- > PersonID
It sounds like you are able to append from Table1 to Table2 without running afoul of any relationship. If that is true, you can empty out Table2, then append all the rows from Table1.
Dim cn As Object
Set cn = CurrentProject.Connection
cn.Execute "DELETE FROM Table2"
If Table2 includes an autonumber field, reset its seed value.
cn.Execute "ALTER TABLE Table2" & vbCrLf & _
"ALTER COLUMN autonum_fld COUNTER(1, 1)"
Then do the append ...
cn.Execute _
"INSERT INTO Table2 (autonum_fld, text_field, long_int_field)" & vbCrLf & _
"SELECT autonum_fld, text_field, long_int_field" & vbCrLf & _
"FROM Table1;"
... or if the two table structures are identical, you don't even have to list the field names ...
cn.Execute "INSERT INTO Table2" & vbCrLf & _
"SELECT *" & vbCrLf & _
"FROM Table1;"
And finally ...
Set cn = Nothing
AFAICT, that could work. However, I don't understand the background details of your question, for example "Access does not allow double relationships". So I may be completely off the mark.