Access

时间:2015-09-14 14:29:21

标签: sql vba ms-access-2010

我遇到阻塞表的问题。 我有一个vba代码。

Public Function delete_fk()
  DoCmd.Close
  DoCmd.RunSQL "alter table transform_tables drop constraint fk_trans;"
End Function

此功能从主宏运行(" test")。它运作良好,没有问题。我在执行主宏的表单中添加了单击按钮" test"。

Private Sub button115_Click()
  [extract tables from transform].test
End Sub

当我点击此按钮时,我收到错误:

  

table" transform_tables"无法被数据库核心阻止。它被其他用户使用。

但是我关闭了所有对象!

任何人都可以帮助我吗?

所有宏代码:

Public Function delete_fk()
DoCmd.Close

' delete primary keys
DoCmd.RunSQL "alter table transform_tables drop constraint fk_trans;"
End Function
Public Function delete_pk()
' ' delete foreign keys
DoCmd.Close

DoCmd.RunSQL "alter table transform_tables drop constraint pk_trans_table_id;"
End Function

Public Function insert_fk()

DoCmd.Close


' insert foreign keys
DoCmd.RunSQL "alter table transform_tables add  constraint fk_trans foreign key (table_id) references tables(id);"
End Function
Public Function insert_pk()

' insert primary keys
DoCmd.Close

DoCmd.RunSQL "alter table transform_tables add constraint pk_trans_table_id PRIMARY KEY (block_id, trans_table);"


End Function
Sub test2()

't = Timer

'checking fk and pk

DoCmd.SetWarnings False
    Dim k  As Integer
    Dim t As Integer
    k = 0
    t = 0
    Set db = CurrentDb
    Set td = db.TableDefs("transform_tables")
    Set t2d = db.TableDefs("transform_tables")

    For Each idxLoop In td.Indexes
    If idxLoop.Primary = True Then
    k = k + 1
    End If
    Next idxLoop
    For Each idxLoop2 In t2d.Indexes
    If idxLoop2.Foreign = True Then
    t = t + 1
    End If
    Next idxLoop2
db.Close
Set td = Nothing
Set t2d = Nothing
Set db = Nothing


' if we have pk then delete.
If k > 0 Then
    ' MsgBox ("k=" k )
     delete_pk
' if we have fk then delete.
End If
If t > 1 Then
     'MsgBox ("t=" & t)

     delete_fk
End If
'now we must add the transform table with some specific
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("transform_tables", dbOpenTable)
Dim lenght As Integer
Dim str As String
Dim pos As Integer



    DoCmd.RunSQL "DELETE transform_tables.block_id, transform_tables.trans_table, transform_tables.Table_id FROM transform_tables"

    Dim format_data As String
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("transformer")
    Dim Form As String

    Dim i As Integer
    Do While Not rs.EOF
        If rs("table_lvl_transform") <> "" Then
        Form = (rs("table_lvl_transform"))

        Dim strPattern As String: strPattern = "(FROM|JOIN)\s+([^ ,]+)(?:\s*,\s*([^ ,]+))*\s*"
        Dim strReplace As String: strReplace = ""
        Dim regEx As New RegExp
        Dim strInput As String


        If Left(rs("table_lvl_transform"), 7) = "PRDN_DB" Then
                      rst.AddNew
                      rst!block_id = rs("block_id")
                      rst!trans_table = rs("table_lvl_transform")
                      rst.Update
        End If

        If strPattern <> "" Then
            With regEx
                .Global = True
                .Multiline = True
                .IgnoreCase = False
                .Pattern = strPattern
           End With


           Set MyMatches = regEx.Execute(Form)

            If MyMatches.Count <> 0 Then
                With MyMatches
                    For myMatchCt = 0 To MyMatches.Count - 1
                            If Left(MyMatches.Item(myMatchCt), 6) <> "FROM (" And Left(MyMatches.Item(myMatchCt), 6) <> "JOIN (" Then

                                 str = MyMatches.Item(myMatchCt)
                                 lenght = Len(str)
                                format_data = Right(str, lenght - 4)
                                 pos = InStr(format_data, ")")
                                 If pos = 0 Then
                                      rst.AddNew
                                      rst!block_id = rs("block_id")
                                      rst!trans_table = format_data
                                      rst.Update
                                 End If
                            End If
                    Next
                End With


           Else

              End If

           End If



    End If
    rs.MoveNext

    Loop
     rst.Close
     Set rst = Nothing

    DoCmd.RunSQL "update transform_tables set trans_table = trim(trans_table);"
    DoCmd.RunSQL ("update transform_tables set trans_table = iif(left(trans_table, 4) = 'PRDN', 'PRD3' & right(trans_table, len(trans_table) - 4), trans_table);")
    DoCmd.RunSQL "update transform_tables set trans_table = iif(left(trans_table, 3) = 'SIT', 'PRD3' & right(trans_table, len(trans_table) - 3), trans_table);"
    DoCmd.RunSQL "update transform_tables set trans_table = switch((InStr(trans_table,'.') = 0) and (trans_table like 't_*' ), 'PRD3_DB_STG.' & trans_table,(InStr(trans_table,'.') = 0) and (trans_table alike '[C,N,S,B][0-9][0-9][0-9]%'), 'PRD3_DB_STG.' & trans_table, (InStr(trans_table,'.') = 0) and (trans_table like 'K_*' or trans_table = 'R_*'), 'PRD3_DB_TMD.' & trans_table, True , trans_table);"
    DoCmd.RunSQL "update transform_tables set trans_table = iif((InStr(trim(trans_table),'.') = 0), 'PRD3_DB_STG.' & trans_table, trans_table);"
    DoCmd.RunSQL "Update transform_tables inner join Tables on (Right(transform_tables.trans_table,Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.'))  = right(tables.tablename, len(tables.TableName) - instr(tables.TableName, '_' ))) Set transform_tables.trans_table = 'PRD3_DB_STG' & tables.TableName where InStr(transform_tables.trans_table, '.') <> 0 and left(transform_tables.trans_table,InStr(transform_tables.trans_table,'.')-1) = 'PRD3_DB_STG' and Right(transform_tables.trans_table,Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.')) not alike '[C,N,S,B][0-9][0-9][0-9]%' and (instr(tables.TableName, '_' ) <> 0) and tables.databaseName = 'PRD3_DB_STG_DDL';"
    DoCmd.Close
    DoCmd.RunSQL "Alter TAble transform_tables add Column keys COUNTER"
    DoCmd.RunSQL "DELETE Transform_tables.keys FROM Transform_tables WHERE (((Transform_tables.keys) Not In (SELECT MIN(keys) FROM Transform_tables GROUP BY trim(trans_table), block_id)));"
    DoCmd.RunSQL "Alter TAble transform_tables drop Column keys"

    DoCmd.RunSQL "insert into tables(databasename, tablename) select distinct left(transform_tables.trans_table,InStr(transform_tables.trans_table,'.')-1) as db, Right(transform_tables.trans_table, Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.')) as tb from transform_tables Left Join(   select A.*  from ( select distinct left(transform_tables.trans_table,InStr(transform_tables.trans_table,'.')-1)  as db, Right(transform_tables.trans_table, Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.')) as tb  from transform_tables ) as A inner join Tables on (A.db  = tables.databasename) and (A.tb = tables.tablename)) as B on  (B.db = left(transform_tables.trans_table,InStr(transform_tables.trans_table,'.')-1)) and (B.tb = Right(transform_tables.trans_table, Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.'))) where (B.db is null) and (B.tb is null);"

    DoCmd.RunSQL "update transform_tables inner join tables on (Right(transform_tables.trans_table,Len(transform_tables.trans_table)-InStr(transform_tables.trans_table,'.')) = tables.tablename) and (left(transform_tables.trans_table,InStr(transform_tables.trans_table,'.')-1) = tables.databaseName) set table_id = tables.id;"
' insert pk and fk
    insert_fk
    insert_pk
'MsgBox ("time is " & Timer - t)
End Sub

0 个答案:

没有答案