导入ACCESS的查询语句到一个表中 直接运行 testCreatequerysql()这个过程就可以把你数据库中的查询导入到一个tblquerysql 这个表是过程建立的,如果过程已经建立了不会覆盖原来的表 Sub testCreatequerysql()
CDtable
CreateQuerySQL "tblquerysql"
End Sub 把查询的语句写入到一个表中的过程 Sub CreateQuerySQL(strTable As String)
On Error GoTo Err_Handler
Dim Rs As DAO.Recordset
Dim qy As DAO.QueryDef
Dim i As Integer
Set Rs = CurrentDb.OpenRecordset(strTable) For Each qy In CurrentDb.QueryDefs
If qy.Name Like "[!~]*" Then
Debug.Print qy.Name
Rs.AddNew
Rs(1) = qy.Name
Rs(2) = qy.SQL
Rs.Update
qy.Close
End If
qy.Close
Next
Set qy = Nothing
Set Rs = Nothing
Err_Handler:
If Err.Number <> 0 Then MsgBox Err.Number & vbCrLf & Err.Description
End Sub 新建一个表tblquerysql可以存放查询语句 Public Sub CDtable()
On Error GoTo error1
Dim db1 As Database
Dim t1 As TableDef
Dim f1 As Field
Set db1 = Workspaces(0).Databases(0)
Set t1 = db1.CreateTableDef("tblQuerySql")
With t1
Set f1 = .CreateField("ID", dbLong)
f1.Attributes = dbAutoIncrField + dbFixedField
Debug.Print f1.Attributes t1.Fields.Append f1
Set f1 = .CreateField("QueryName", dbText, 50)
' f1.Required = True
' f1.AllowZeroLength = False
' f1.ValidationRule = "is not null"
' f1.ValidationText = "不允许为空" t1.Fields.Append f1
Set f1 = .CreateField("SqlValue", dbMemo)
t1.Fields.Append f1 Set f1 = .CreateField("operTime", dbDate)
f1.DefaultValue = "=Now()"
t1.Fields.Append f1
End With
db1.TableDefs.Append t1
db1.Close
Exit Sub
error1:
If Err.Number = 3010 Then
Exit Sub
Else
MsgBox Err.Number & Err.Description
Exit Sub
End If
End Sub |