【问题标题】:How to check Table exists in MS Access through VBscript如何通过VBscript检查MS Access中是否存在表
【发布时间】:2015-11-24 05:44:52
【问题描述】:

我有一个 vbscript 试图在 MS Access DB 中创建表,但我想让它像如果表存在,那么它将直接继续输入数据而无需创建表。

我可以做些什么来检查表的存在是否被创建?

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

'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"

【问题讨论】:

标签: vbscript


【解决方案1】:

或者像这个:

Function TableExists(strTableName)
    Dim RStmp
    TableExists = true
    on error resume next
    RStmp=Conn.Execute("SELECT * FROM ["&strtablename&"]")
    If Err.Number <> 0 Then TableExists=false
    on error goto 0
End Function

【讨论】:

    【解决方案2】:

    使用这样的函数:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多