如何在 VB 6.0 上运行插入查询?

  • 本文关键字:运行 插入 查询 VB vb6
  • 更新时间 :
  • 英文 :


我是Visual Basic 6.0的新手,我不知道如何编写SQL查询,特别是Insert Into

我有 1 个数据库和 2 个表(tblNames、tbl备注(。

在 tbl名称中,字段为:

ID, LastName, FirstName, MidName, DateHired, Position, Department

在 tbl备注 中,字段为:

ID, FullName, txtDate, Remarks, DateHired, Position

我正在做的是这样的:

SQL =  "SELECT ID, LastName, FirstName, MidName, DateHired, Position FROM tblNames"
SQL2 = "SELECT * fROM tblRemarks"

然后在我的表单上,我有一个DTPicker1命令 1

我想获取 tblNames 中的所有记录并将其放在 tblRemarks 上,但也要从 DTPicker1 和字段备注中获取日期,即"HOLIDAY"一词。

我正在做的是这样的:om 我的模块我有这段代码:

Option Explicit
Public conn As New ADODB.Connection
Public RS As New ADODB.Recordset
Public cmd As New ADODB.Command
Public SQL As String
Public SQL2 As String
Public Sub connectDB()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
With RS
.ActiveConnection = conn
.Open SQL, conn, 3, 3
End With
End Sub
Public Sub connOpen()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = SQL
Set RS = .Execute
End With
End Sub

然后我只是在我的表格中调用它

我的问题是,我有 2 个 SQL 查询,如何在连接中连接这 2 个 SQL?我研究了它,发现我可以使用插入进行 1 个查询。

我试过了,但我无法让它工作。我怎样才能做到这一点?我对VB6很陌生。会是这样吗?

SQL = "INSERT ID, LastName, FirstName, MidName, DateHired, Position FROM tblNames" _
& "INTO tblRemarks, #" & DatePick & "# as txtDate, 'HOLIDAY' as Remarks"

完全未经测试,但也许您可以尝试以下方法

conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "Database.accdb;User ID=admin;Persist Security Info=False;JET OLEDB:Database Password=qqqq"
conn.Open
SQL = "INSERT INTO tblRemarks " & _
"(FullName, txtDate, Remarks, DateHired, Position) " & _
"SELECT CONCAT_WS(' ', FirstName, MidName, LastName), ?, 'HOLIDAY', DateHired, Position " & _
"FROM tblNames"
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = SQL
.Parameters.Append .CreateParameter(, adVarWChar, adParamInput, 100, DatePick)
.Execute
End With

100替换为txtDate列的宽度。

相关内容

  • 没有找到相关文章

最新更新