如何检查表通过VBscript存在于MS Access中

时间:2015-11-24 05:44:53

标签: vbscript

我有一个尝试在MS Access DB中创建表的vbscript,但是我想让它就像表存在一样,然后它将直接继续输入数据而不需要创建表。

我该怎么做来检查表的现有是否已创建?

我的代码如下,如果表已经存在,它将不会继续插入数据。

'Constants
'Const adOpenStatic = 3
Const adOpenDynamic = 2
Const adLockOptimistic = 3
Const adCmdTable = &H0002

Set objConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

'Connect Primary DB
connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & "C:\AIS_Workfolder\Reference\DB\" & "AIS_DataDB.mdb"

'Open Connection
objConn.open connStr

'Create table
objConn.Execute "CREATE TABLE " & "test_table" & "(" & _
  "[ID] COUNTER ," & _
  "[Field1] TEXT(255) ," & _
  "[Field2] TEXT(255) ," & _
  "[Field3] TEXT(255) ," & _



objRecordSet.Open "test_table", objConn, adOpenDynamic, adLockOptimistic, adCmdTable

    objRecordSet.AddNew
      objRecordSet("Field1").value = "testing123"
      objRecordSet("Field2").value = "testing123"
      objRecordSet("Field3").value = "testing123"

2 个答案:

答案 0 :(得分:1)

或者喜欢这个:

SELECT ?g ?s ?p ?o
WHERE {
{ ?s ?p ?o }
UNION
{ GRAPH ?g { ?s ?p ?o } } }

答案 1 :(得分:0)

使用这样的函数:

If TableExists("test_table") Then
  ' Take action here
Else    
  ' Create table here
End If    

Function TableExists(TabletoFind)
    TableExists = False
    Set adoxConn = CreateObject("ADOX.Catalog")
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open(ConnStr)
    adoxConn.ActiveConnection = objConn
    IsThere = False
    For Each Table in adoxConn.Tables
        If LCase(Table.Name) = LCase(TabletoFind) Then
            IsThere = True
            Exit For
        End If
    Next
    objConn.Close
    Set objConn = Nothing
    Set adoxConn = Nothing
    If IsThere Then TableExists = True
End Function