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
Related
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") & "')"
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.
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].
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 & "'";
I have an Excel sheet in which the first row contains title for all the columns. I want all the names in the first row.
What is the SQL command for reading all the entries in the first row?
If possible, I want to define the max-limit.
In addition: I want to enumerate all the column names.
You need to mention "HDR=No;" in your connection string. Then you can use the following query:
Select Top 1 * from [SheetName$]
"HDR=No" will specify that the first row DOES NOT contains the column names.
Being said that, I don't know if there is a SQL statement to get the column names from a table. If there is, then you can have HDR=Yes in the connection string and use that SQL statement.
I hope I'm understanding this right....but I think you're saying that you want to select the column names from a table:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = [YourTableName]
This works for me in Excel using a saved workbook, and it enumerates the column (field) names.
Sub ListFieldADO()
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
''You can also use the name of a named range
Set rs = cn.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, "Sheet1$"))
While Not rs.EOF
Debug.Print " " & rs!Column_Name
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
End Sub
Select Top 1 * into #temp from [SheetName$]
use tempdb
sp_help #temp.
By this, you can get the column names of the #temp table. Again, you have to change the database to:
use yourDBName
Put the data in a temporary table and read the properties of the temporary table and from that you can get the list of column names.