Results 1 to 18 of 18

Thread: look for table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    look for table

    hi,
    what is the best way (and the shortest ) to see if the table is exiting in the database
    omer

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Quote Originally Posted by omerg84
    hi,
    what is the best way (and the shortest ) to see if the table is exiting in the database
    omer
    Which database??
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    regular database in access,with tables

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Quote Originally Posted by omerg84
    regular database in access,with tables
    The easiest way would be to check it using the Query
    PHP Code:
    Select From MSysObjects Where [NAME] = 'TABLENAME' 
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    does it works????

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Quote Originally Posted by omerg84
    does it works????
    If it wouldn't have been working then I wouldn't have posted it.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    i need example of using this statment
    thanks,omer

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Open a recordset using the above posted query and check if the recordset returns any records.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    anyone can give me an example how to open recordset with the quary:Select * From MSysObjects Where [NAME] = 'TABLENAME'
    in vb
    omer

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    another problem,i can't read from msysobjects beacuse i have security problem..how can i allow the program to read from msysobjects?
    thanks
    omer

  11. #11
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Here is a sample of how this can be done
    VB Code:
    1. Dim cn As ADODB.Connection
    2. Set cn = New ADODB.Connection
    3. cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myMDB.mdb"
    4. Dim rs As ADODB.Recordset
    5. Set rs = New ADODB.Recordset
    6. rs.Open "Select * From MSYSOBJECTS Where [NAME] = 'myTable'"
    7. If rs.EOF Then
    8.    Msgbox "Table dooes not Exist"
    9. Else
    10.    MsgBox "Table Exists"
    11. End If
    12. rs.Close
    13. Set rs = Nothing
    14. cn.Close
    15. Set cn = Nothing
    You will need to add a reference to Microsoft ActiveX Objects 2.x Library.

    What is the error message that you are getting?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    the error is that i don't have promision to read from msysobject
    (i did the code with regular dao and it works...not with adodb)

  13. #13
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    Quote Originally Posted by omerg84
    the error is that i don't have promision to read from msysobject
    (i did the code with regular dao and it works...not with adodb)
    Can you show us what you did?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    i opened a record with the quary like:
    "Select * From MSYSOBJECTS Where [NAME] = 'myTable'"
    and it shows an error that i don't have promission to read from MSYSOBJECTS
    how can i solve it?
    omer

  15. #15
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: look for table

    That is not the code that I had asked for. How are you doing it in VB? What is the connectionstring, etc??
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  16. #16
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Re: look for table

    I use a different trick by checking the success / failure of an special SQL statement that only returns one row. I am aware that any database error will also show up as a failure. The advantage is that I don't rely on database schema permissions:

    VB Code:
    1. '-------------------------------------------------
    2. ' Check if a table or field exists in the database
    3. '-------------------------------------------------
    4. Public Function TableFieldExist(Table As String, Field As String)
    5.  
    6.     Dim SqlCmd As String
    7.    
    8.     On Error Resume Next
    9.    
    10.     If Field = "" Then
    11.         SqlCmd = "select count(*) from " & Table
    12.     Else
    13.         SqlCmd = "select count(" & Field & ") from " & Table
    14.     End If
    15.    
    16.     ' Verifier si la table ou le champ existe
    17.     Set rsTemp = dbsdata.Execute(SqlCmd)
    18.     If Err.Number = 0 Then
    19.         rsTemp.CloseRecordset
    20.         Set rsTemp = Nothing
    21.         TableFieldExist = True
    22.     Else
    23.         TableFieldExist = False
    24.     End If
    25.    
    26. End Function

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    93

    Re: look for table

    thanks but i need example for how to check the exsiting of table,not field

  18. #18
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Re: look for table

    With my function:

    VB Code:
    1. ' Check if a table exists
    2. if TableFieldExist("Customers", "") then ...
    3.  
    4. ' Check if a field exists
    5. if TableFieldExist("Customers", "CustName") then ...

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