【发布时间】:2015-02-26 05:18:38
【问题描述】:
VBA 很新,而且在互联网上似乎没有太多帮助,因为我知道它是相对旧的。
我正在尝试使用宏将数据从 Excel 工作表提交到 SQL 服务器数据库。
基本上单击一个按钮,应该从单元格中提取所需的数据,然后将数据放在我的数据库中的正确列中。
它没有正确提交数据,例如一个单元格中有数字“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 中找到的相同值并将其设置为数据库记录的每一列的值。这是您想要的吗?
-
只是试图将“D5”单元格值添加到属于 [header] 表的 ,[ip_address] 列。