Results 1 to 6 of 6

Thread: Validating a table

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Validating a table

    How do I validate if a table named mytb is empty? I would like to decalare the code as
    Code:
    if mytb.empty = true then
    ...
    but this is not possible.

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    You would have to either a) open a recordset of the contents of the table and check the .RecordCount property or b) run a

    SELECT COUNT(*) AS RecordCount FROM MyTB

    into a recordset and check the RecordCount field

    Why not make a quick function;

    Function EmptyTable(TblName As String) As Boolean
    Dim tmpRS As New ADODB.Recordset
    TmpRs.Open "SELECT COUNT(*) AS RecordCount FROM "+TblName
    EmptyTable = Tmprs.Fields("RecordCount")=0
    Tmprs.Close
    End Function

    Then you can use

    If EmptyTable("MyTB") Then...
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    OK, but an error occurs running your code. It's an RTE 3709.

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Oops - I forgot the ADO connection; try this;

    Code:
    Function EmptyTable(TblName As String, ADOConn as ADODB.Connection) As Boolean 
    Dim tmpRS As New ADODB.Recordset 
    TmpRs.Open "SELECT COUNT(*) AS RecordCount FROM "+TblName, AdoConn, AdOpenStatic, AdLockReadOnly
    EmptyTable = Tmprs.Fields("RecordCount")=0 
    Tmprs.Close 
    End Function
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Ok, what is then the statement for
    If EmptyTable("MyTB", ???) Then...

    ?

  6. #6
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    You need to pass it a valid ADO connection;

    so if you had done this

    Dim MyConn AS New ADODB.Connection
    MyConn.Open ".........."

    You would use

    If EmptyTable("MyTB", MyConn) Then...
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width