Sub dooTables(strTable)
Dim db as Database, tdf as TableDef
Dim sSQL as String
For Each tdf in db.TableDefs
If tdf.Name = strTable Then
'Gör något
else
'Gör ngt annat
end if
Next tdf
End Sub
dooTables("123")
Function fnExistTable(strTableName As String) As Boolean
Dim db As Database
Dim i As Integer
Set db = DBEngine(0)(0)
fnExistTable = False
db.TableDefs.Refresh
For i = 0 To db.TableDefs.Count - 1
If strTableName = db.TableDefs(i).Name Then
fnExistTable = True
Exit For
End If
Next i
Set db = Nothing
End Function
If fnExistTable("123") Then
[blue]'Tabellen finns[/blue]
Else
[blue]'Tabellen finns inte[/blue]
End If
Hur du får det här att fungera i ASP har jag ingen aning om ;)
<%
Dim objConn
Dim rstTables
Dim strTableName
strTableName = "TBL_MEMBERS"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "MyConnString"
Set rstTables = objConn.OpenSchema(20)
If Not rstTables.EOF Then
Do Until rstTables.EOF
If LCase(rstTables("TABLE_NAME")) = LCase(strTableName) Then
'## -- tabellen fanns --
Response.Write "Tabellen " & strTableName & " finns i databasen."
Exit Do
End If
rstTables.MoveNext
Loop
End If
Set rstTables = Nothing
Set objConn = NOthing
%>
Public Function TableExists( strTableName, strDBPath )
Dim oCon
Dim oRs
Dim intExists
Set oCon = Server.CreateObject("ADODB.Connection")
oCon.open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & strDBPath & ";"
Set oRs = oCon.OpenSchema(20)
intExists = false
Do while Not oRs.EOF
If Left(oRs.Fields("TABLE_NAME"),4) <> "MSys" Then
If strTableName = oRs.Fields("TABLE_NAME") Then
intExists = true
Exit Do
End If
End If
oRs.MoveNext
Loop
Set oRs = Nothing
oCon.close
Set oCon = Nothing
If intExists Then
TableExists = True
Else
TableExists = False
End If
End Function
Function TableExists(ByVal sTableName, ByRef cnn) ' As Boolean
Dim rstTables ' As Recordset
Set rstTables = cnn.OpenSchema(20)
If Not rstTables.EOF Then
Do Until rstTables.EOF
If LCase(rstTables("TABLE_NAME")) = LCase(sTableName) Then
'## -- tabellen fanns --
TableExists = True
Set rstTables = Nothing
Exit Function
End If
rstTables.MoveNext
Loop
End If
Set rstTables = Nothing
End Function
bättre att passa in connection-objectet som en byref argument istället för att öppna en ny connection endast för att kika på om tabellen finns eller inte.