|
- Public Function tblExist(tblName As String) As Boolean
- Dim tbl As TableDef
- tblExist = False
- For Each tbl In CurrentDb.TableDefs
- If tbl.Name = tblName Then
- tblExist = True
- Exit For
- End If
- Next
- End Function
复制代码- Private Sub Command0_Click()
- Dim rs As New ADODB.Recordset
- Dim cnn As New ADODB.Connection
- Dim strFld() As String
- Dim strSQL As String
- Dim i As Integer
- Set cnn = CurrentProject.Connection
- If tblExist("A") Then
- DoCmd.DeleteObject acTable, "A"
- End If
- rs.Open "标题", cnn, adOpenKeyset, adLockReadOnly
- ReDim strFld(1 To rs.Fields.Count)
- For i = 1 To rs.Fields.Count
- strFld(i) = rs.Fields(i - 1)
- Next
- rs.Close
- rs.Open "表", cnn, adOpenKeyset, adLockReadOnly
- For i = 1 To rs.Fields.Count
- strSQL = strSQL & "字段" & i & " as " & strFld(i) & ","
- Next
- If Len(strSQL) <> 0 Then
- strSQL = Left(strSQL, Len(strSQL) - 1)
- End If
- strSQL = "select " & strSQL & " into A from 表"
- CurrentDb.Execute strSQL
- DoCmd.OpenTable "A"
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|