Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
VB.NET
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
           Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\POSdata.mdb; Jet OLEDB:Database Password=pos1234")

           AccessConn.Open()

       Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Items1] FROM [Excel 8.0;DATABASE=C:\Book1.xls;HDR=NO;IMEX=1].[Sheet1$]", AccessConn)

           AccessCommand.ExecuteNonQuery()
           AccessConn.Close()
           MsgBox("OK")
   End Sub


What I have tried:

Hi! If I change " SELECT * INTO [Sheet1] " then when click this button11 this code is ok but when is saved to new table in access. It need to save data in " Items1 " but when I tried this code " SELECT * INTO [Items1] " then is showing this problem for me:

Table 'Items1' already exists.

How to solve this problem to upload data in table " Items1 " and no in to a new table ?
Please help :) Thanks
Posted
Updated 11-May-16 2:23am

SELECT * INTO will create the table. If the table already exists just use
SQL
INSERT INTO tableName 
SELECT ...
 
Share this answer
 
Comments
ionMEMBER 11-May-16 8:19am    
Syntax error in INSERT INTO statement.
Please can you explain me to the long way? thanks
ZurdoDev 11-May-16 8:21am    
This is something very easily looked up, faster than waiting for us to respond.

INSERT INTO tableName (field1, field2, field3)
SELECT field1, field2, field3
FROM sometable
WHERE someCondition

OR
INSERT INTO tableName (field1, field2, field3)
VALUES (@value1, @value2, @value3)
ionMEMBER 11-May-16 8:23am    
ok. thank you very much ! :D :)
ZurdoDev 11-May-16 8:25am    
You're welcome. If you get syntax errors just work through them. You'll learn more that way. But if you do get completely stuck, open a new question and we'll gladly help.
You could issue a
SQL
drop table Items1
within a Try-Catch block - ignore the error if it's saying the table does not exist. Not very elegant but it will work most of the time.

There is a "hidden" table that you could query
SELECT COUNT(*) as t from MsysObjects WHERE type in (1,4,5,6) AND name = 'Items1' 

Drop the table if t is > 0.

Or, create the table explicitly (you will need to know up front what is on the sheet) and swap that query to be
SQL
INSERT INTO Items1 SELECT * FROM [Excel 8.0;DATABASE=C:\Book1.xls;HDR=NO;IMEX=1].[Sheet1$]

There will be issues with the last approach if the users move any columns around (in my experience, they will do this)

Comment - these approaches are intended to work for when the table does not exist at all (first run of program) AND for subsequent runs. The first two suggestions assume that you do not want to retain any data from previous runs. The last suggestion will retain data from previous runs (and is subject to the vagaries of users moving columns around)
 
Share this answer
 
v2
Comments
ionMEMBER 11-May-16 8:33am    
#Chill60
I tried this: INSERT INTO Items1 SELECT * FROM [Excel 8.0;DATABASE=C:\Book1.xls;HDR=NO;IMEX=1].[Sheet1$]

Problem: The INSERT INTO statement contains the following unknown field name: 'F1'. Make sure you have typed the name correctly, and try the operation again.
CHill60 11-May-16 8:35am    
As I said - you will need to know up front what is on the sheet and have already created the table accordingly
ionMEMBER 11-May-16 8:57am    
Again I not give solution. Please can I help me to send a link with a simple project? please...
thank you!
CHill60 11-May-16 9:20am    
I strongly suggest that you remove your email from this open forum otherwise you could end up being the target of spam.
I will only answer questions via this site, not by private email.
ionMEMBER 11-May-16 9:20am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900