---
title: "Finns tabellen? (...mot Access)"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/72177-finns-tabellen-mot-access"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "Dee"
published: "2003-03-25T23:20:56.000Z"
updated: "2003-03-26T16:02:10.000Z"
replies: 10
views: 332
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/72177-finns-tabellen-mot-access"
---

# Finns tabellen? (...mot Access)

## #1 — Dee, 2003-03-25T23:20Z

Är det någon som har nått förslag på hur man kan gå till väga för att kolla om en viss tabell finns i databasen?

```
If tabell med namn '123' finns i db.mdb
   '
   'Gör detta...
   '
Else
   '
   'Gör detta...
   '
End If
```

Permalänk: https://www.webforum.nu/p/72177

## #2 — Jesper T, 2003-03-26T00:01Z

Något sånt här kanske: 

```
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")
```

 (Totalt otestat)
Titta på dessa länkar:
<http://www.webforum.nu/showthread.php?s=&threadid=66097&highlight=tabellnamn>
<http://www.webforum.nu/showthread.php?s=&postid=60883#post60883>

Permalänk: https://www.webforum.nu/p/990894

## #3 — LarsG, 2003-03-26T00:13Z

Varför vill du göra en sån sak i din appplikation? Att bygga in sådana beroenden i applikationen är oftast fel väg att gå.

Permalänk: https://www.webforum.nu/p/990897

## #4 — Engine^, 2003-03-26T09:53Z

Så här kan du göra i Access:

```
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 ;)

Permalänk: https://www.webforum.nu/p/991017

## #5 — PatrikB, 2003-03-26T10:31Z

Denna funkar i ASP:

```
<%
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
%>
```

cya,
PatrikB

Permalänk: https://www.webforum.nu/p/991047

## #6 — Vide, 2003-03-26T10:59Z

Eller så gör man en funktion av det. :)

```
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
```

Permalänk: https://www.webforum.nu/p/991066

## #7 — OveRRidE, 2003-03-26T11:55Z

Många förslag här nu. ;)

Permalänk: https://www.webforum.nu/p/991109

## #8 — PatrikB, 2003-03-26T12:06Z

ok, ett tips som funktion:

```
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.

cya,
PatrikB

Permalänk: https://www.webforum.nu/p/991121

## #9 — Engine^, 2003-03-26T12:08Z

Låter klokt :)

```
If Not rstTables.EOF Then ... End If
```

är väl helt onödig?

Permalänk: https://www.webforum.nu/p/991126

## #10 — PatrikB, 2003-03-26T12:16Z

jo, den följer med av gammal vana .... men, den behövs inte i detta fallet :e

cya,
PatrikB

Permalänk: https://www.webforum.nu/p/991130

## #11 — Dee, 2003-03-26T16:02Z

Tack för alla svar.  :) 
Nu har jag lärt mig nått nytt idag också.

> **OveRRidE skrev:**
>
> Många förslag här nu. ;)

Det underbara med wF...  :h

Permalänk: https://www.webforum.nu/p/991328

---

Tråden på webben: https://www.webforum.nu/amne/asp/72177-finns-tabellen-mot-access
