與VBA相當新,似乎沒有太多的互聯網上的幫助,因爲我明白它是相對舊的。VBA和Excel宏
我想使用宏將數據從Excel表提交到SQL服務器數據庫。
基本上單擊一個按鈕,應該從單元格中提取所需數據,然後將數據放入我的數據庫中的正確列中。
它沒有提交數據properley,例如一個單元格中有數字「2」,它將數字「0」提交到我的數據庫中。
任何人都可以建議嗎?下面
代碼。
' Create the connection string.
sConnString = "provider=xxx; Data Source=xx-xxx; Initial Catalog=xxx;User ID= xx; Password=xxx;": MsgBox "Connection Succesful"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim val As String
val = Range("D5").Value
' Open the connection and execute.
conn.Open sConnString
Dim item As String
item = "INSERT INTO [Industrial].[dbo].[Header]("
item = item & " [server_name]"
item = item & " ,[date]"
item = item & " ,[amendee]"
item = item & " ,[ip_address]"
item = item & " ,[physical_location]"
item = item & " ,[host_name]"
item = item & " ,[is_it_contact]"
item = item & " ,[businesscontact]"
item = item & " ,[businessdependencies]"
item = item & " ,[backup_strategy_in_place]"
item = item & " ,[physorvirt]"
item = item & " )Values("
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
item = item & " '" & val & "'"
conn.Execute item
末次
看起來像是在D5中找到相同的值,並將其設置爲數據庫記錄每列的值。是您想要的嗎? – Jaycal
只是試圖將'D5'單元格值添加到屬於[header]表中的[ip_address]列中。 – user3013325