How to check if Table exist

itayzoro

Member
Joined
Jul 28, 2008
Messages
23
Programming Experience
Beginner
How to check if Table exist and if so a Field exist and if so is containe any records?

so far this what i have :


VB.NET:
Dim sConnectionString, SQL24 As String
Dim dbFileNew As String = "C:\Dest.mdb"
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFileNew
SQL24 = "SELECT totalf AS Expr1 FROM TOTAL"
Dim Sconn As New System.Data.OleDb.OleDbConnection(sConnectionString)
Dim cmd24 As New System.Data.OleDb.OleDbCommand(SQL24, Sconn)
Dim dr24 As System.Data.OleDb.OleDbDataReader

        Try
            Sconn.Open()
            dr24 = cmd24.ExecuteReader()
            dr24.Read()
       If dr24.FieldCount = 0 Then
                MsgBox("No Field")
            Else
                MsgBox(dr24.Item("Expr1")) 'MsgBox("Exist")
            End If

            dr24.Close()
            Sconn.Close()
        Catch

        End Try
 
just attempt to select the count(field) of it! It will error out if the table or column doesnt exist, it will return a null or 0 if the column is all null, or it will return a count if any items are not null. i dont reccommend doing it often, as it needs a full table scan in the case of an unindexed column and that will be slow
 
Back
Top