MsgBox中的SQL记录

时间:2016-10-26 14:25:00

标签: sql database sqlite vbscript msgbox

我有一个很棒的VBS程序。我的代码如下:

Dim qry, db, cs, cn, cmd

'Query to add all the following which date more than 20 days in the table ToUnfollow
qry  = "INSERT INTO ToUnfollow " & _
       "SELECT Name FROM Follow t1 " & _
       "WHERE t1.Joined < datetime(CURRENT_DATE, '-20 days') " & _
       "AND NOT EXISTS (SELECT 1 FROM ToUnfollow t2 WHERE t2.Name=t1.Name);"

db = "C:\Users\Quentin\Downloads\Quentin-Classementhashtags.db"
cs = "DRIVER=SQLite3 ODBC Driver;Database=" & db

'Connection to database
Set cn = CreateObject("ADODB.Connection")
cn.Open cs

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn

'Execute the SQL query
cmd.CommandText = qry
cmd.Execute

'Close the connection
cn.Close

我想在MsgBox中显示SQL查询中的所有记录。我试过几个论坛的几个解决方案,但没有一个适合我。

1 个答案:

答案 0 :(得分:1)

我的解决方案:

Dim qry, db, cs, cn, cmd, adoRec, name

'Query to add all the following which date more than 20 days in the table
'ToUnfollow
qry  = "SELECT Name, Joined FROM Follow t1 " & _
       "WHERE t1.Joined < datetime(CURRENT_DATE, '-20 days')"

db = "C:\Users\Quentin\Downloads\Quentin-Classementhashtags.db"
cs = "DRIVER=SQLite3 ODBC Driver;Database=" & db

'Connection to database
Set cn = CreateObject("ADODB.Connection")
cn.Open cs

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn

cmd.CommandText = qry
Set adoRec = cmd.Execute()

'Iterate through the results
While Not adoRec.EOF
  name = name & vbCr & adoRec(0) & " - " & adoRec(1)
  adoRec.MoveNext
Wend

MsgBox name

'Close the connection
cn.Close