I'm using an OleDbDataReader to loop through my imported excel file and it gets everything in the file except for the first row. How can I specify that I want to see this header row?
strSQL = "SELECT * FROM [" & firstSheetName & "]"
Dim myCommand As New OleDbCommand(strSQL, myConnection)
Dim myDba As OleDbDataReader
myDba = myCommand.ExecuteReader
s = ""
While myDba.Read()
For i = 0 To myDba.FieldCount - 1
s &= myDba.Item(i) & vbTab
Next
s &= vbCrLf
End While
You should specify that your spreadsheet doesn't have header: HDR=No .
Related
I am trying to import via a VBA button a ton of Excel data (around 30k + daily) into an existing table in SQL server. My question is how can I do this as simple as possible, code speaking?
The headers both in my Excel file and SQL table are 1:1 the same so I just want to import everything into the SQL table
This is what I started to write but when I try to make the code work I get a "Run-time error '-2147217865 (80040e37): Invalid object name "dbo.Rawdata".
Private Sub cmdImport_Click()
Dim r As Range
Dim c As Range
Set r = Sheet1.Range("A6:DA269239")
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.ConnectionString = _
"Provider=MSOLEDBSQL;" & _
"Server =localhost\name" & _
"Database =name;" & _
"Trusted_Connection=yes;"
con.Open
Dim iRowNo As Integer
Dim strn_reference As String
Dim batchInsert As String
Dim batchSize As Integer
batchSize = 1000
iRowNo = 0
For Each cl In r
iRowNo = iRowNo + 1
batchInsert = batchInsert + (IIf(iRowNo > 1, ",", "")) + "('" & Replace(cl.Value2, "'", "''") & "')"
If (iRowNo = batchSize) Then
con.Execute "insert into dbo.Rawdata (trn_reference) Values " & batchInsert
iRowNo = 0
batchInsert = ""
End If
Next
If Len(batchInsert) > 0 Then con.Execute "insert into dbo.Rawdata (trn_reference) Values " & batchInsert
MsgBox "Reference Numbers imported"
con.Close
Set con = Nothing
End Sub
Thank you everyone for the help!
I guess you should refer to the table name from SQL server not dbo.Rawdata but directly:
Rawdata|"Insert into Rawdata(column_name) VALUES ('" & vba_variable & "')"
This should be the SQL statement from VBA.
This work for me very well.
How to convert recordset to dataset because while 200 more data load to datagridview that after datagrid has been very slow, so i want my code change to load datagrid by dataset but i dont know dataset code how to merge multiple field of database data into one column in datagrid, So please help me...
Private Sub FLEX1_LOAD()
DataGridView1.Rows.Clear()
Dim strsql As String
Dim rs2 As ADODB.Recordset
strsql = "select * from voterlist where AREAID='" & Trim(lblAreaid.Text) & "'"
rs2 = New ADODB.Recordset
Call dbconnect2()
rs2.Open(strsql,conne2,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockPessimistic)
If rs2.RecordCount > 0 Then
rs2.MoveFirst()
For I = 0 To rs2.RecordCount - 1
DataGridView1.Rows.Add(1)
DataGridView1.Rows(J).Cells(0).Value = J + 1
DataGridView1.Rows(J).Cells(1).Value =rs2.Fields("ID").Value
DataGridView1.Rows(J).Cells(2).Value = rs2.Fields("NAM").Value & " " & rs2.Fields("SURNAME").Value & "/" & rs2.Fields("RTYPE").Value & "-" & rs2.Fields("RNAME").Value & " " & rs2.Fields("RSURNAME").Value
DataGridView1.Rows(J).Cells(3).Value = rs2.Fields("AGE").Value
J = J + 1
rs2.MoveNext()
Next
End If
rs2.Close()
rs2 = Nothing
conne2.Close()
End Sub
I'm trying to make an update clause with an Excel sheet that contains about 2958 rows (32538 cells in total). First I'm identifying which cells contain incorrect data (for example, a cell with characters in a numeric column) and then replace these incorrect data with value that I want.
With small Excel sheets it works fine, but when viewing a larger sheet, the program freezes after some time.
How can I make fast update queries and handle a lot of data?
PD: Sorry for not leaving the code of the update . Here is how I am doing .
Protected Friend Sub reemplazarDato(ByVal columna As String, ByVal data As String, ByVal con As String)
Dim cmd As String = ""
For Each itm In arrayErrores
cmd = "UPDATE [" & obtenerHojaActual(columna, con) & "$] SET [" & columna & "]='" & dato & "' WHERE [" & columna & "]='" & itm & "'"
Try
conexion.Open()
Dim comando As New OleDbCommand(cmd, conexion)
comando.ExecuteNonQuery()
comando.Dispose()
conexion.Close()
Catch ex As Exception
repairmanMessage("Error inesperado", ex.Message, My.Resources._error).ShowDialog()
conexion.Close()
End Try
Next
End Sub
I need to copy structure of selected database to a new database. How is it possible? I am not able to retrieve the DataSet value. I need to use this value to retrieve the table names associated with the database. Here is my code.
cmd.CommandText = "create database " & txtCustId.Text & "AAA"
cmd.ExecuteNonQuery()
Dim dtadapt As New SqlDataAdapter("select table_name from " & name & ".INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE'", comm_con)
Dim dtset As New DataSet
dtadapt.Fill(dtset)
Dim dttb As DataTable = dtset.Tables(0)
'Dim row As Integer = 1
Dim arrS As String = ""
while dttb.Rows.Count > 0
'arrS =Convert.ToString(dtset.Tables[i].Rows[0]["table_name"])
arrS = dttb("table_name").ToString
cmd.CommandText = " SELECT * INTO " & txtCustId.Text & "AAA.dbo." & arrS & " FROM " & name & ".dbo." & arrS & " where 1=1"
cmd.ExecuteNonQuery()
'MessageBox.Show("done")
End while
Please could you help me sort this by 2nd column descending?
Dim ds As New DataSet()
conn.Open()
techSpeciality = LB_techFaultType.Text
techZone = LB_TechZone.Text
Dim strSQL As String = "SELECT Tech_ID, Last_Zone FROM Technician_List WHERE Availability=TRUE AND Speciality='" & techSpeciality & "' AND Last_Zone='" & techZone & "'"
Dim da As New OleDbDataAdapter(strSQL, conn)
da.Fill(ds)
'2D array
Dim values(ds.Tables(0).Rows.Count - 1, 2) As Integer
'for loop between 0 and all available technicians
For value As Integer = 0 To ds.Tables(0).Rows.Count - 1
'Tech_ID column
values(value, 0) = ds.Tables(0).Rows(value).Item(0).value()
'Zone column, converts negative value to a positive and minus' the selected techZone
Math.Abs(values(value, 1) = techZone - ds.Tables(0).Rows(value).Item(1).value())
Next
Modify this line:
Dim strSQL As String = "SELECT Tech_ID, Last_Zone" & _
" FROM Technician_List WHERE" & _
" Availability=TRUE AND Speciality='" & techSpeciality & _
"' AND Last_Zone='" & techZone & _
"' ORDER BY Last_Zone DESC"
For more information on the ORDER BY keywords:
http://www.w3schools.com/sql/sql_orderby.asp
Another option is to do ORDER BY 2 DESC, instead of directly specifying the column name.