Skip to main content

AccessTr.neT


Tablo Oluşturmadan Önce Eskisini Varsa Kontrol Etmek

Tablo Oluşturmadan Önce Eskisini Varsa Kontrol Etmek

Çözüldü #1
iyi günler 
create Sql komutu ile komutu ile tablo oluşturuyoruz ama diyelimki hali hazırdaki tablo bizim istediğimiz alanlara sahip değil veya yok
bunu sorgulayıp tablo oluşturabilir miyiz
mesela oluşturmak istediğimiz tablo ile aynı ada sahip fakat alakasız alanları olan bir tabloyu silip yeni tablo oluşturabilir miyiz
mantık olarak şöyle soracak 
bu isimli bir tablo var mı
varsa alanları nelerdir
uygunsa veri ekleme kısmına geç
uygun değilse yeni isimde mi oluşturulsun veya 
silinip yeni tablo oluşturulsun
@benbendedeilem
Cevapla
#2
şöyle bir kod var buna alan kontrolu sorgulama nasıl eklenir


Kod:
Sub ViaVBA()
    Const strSQLCreateFoo_c As String = _
          "CREATE TABLE Foo" & _
          "(" & _
          "MyField1 INTEGER," & _
          "MyField2 Text(10)" & _
          ");"
    Const strSQLAppendBs_c As String = _
          "INSERT INTO Foo (MyField1, MyField2) " & _
          "SELECT Bar.MyField1, Bar.MyField2 " & _
          "FROM Bar " & _
          "WHERE Bar.MyField2 Like 'B*';"

    If Not TableExists("foo") Then
        CurrentDb.Execute strSQLCreateFoo_c
    End If
    CurrentDb.Execute strSQLAppendBs_c
End Sub

Private Function TableExists(ByVal name As String) As Boolean
    On Error Resume Next
    TableExists = LenB(CurrentDb.TableDefs(name).name)
End Function

Son Düzenleme: 01/02/2020, 10:02, Düzenleyen: accessman.
Cevapla
#3
Ado schmea ile tablo adları bulunuyordu.
Belki orda Alan adlarıda yazıyordur incelemek gerek aklıma geldi.
Cevapla
#4
Alttaki gibi bir kod var ve Tablodaki alan adlarını bulyor.
Ona bir if koşulueklersen sarım  istediğin olur.


Dim sTable As String
    Dim rs As DAO.Recordset
    Dim n As Long
    Dim sResult As String


    Set rs = CurrentDb.OpenRecordset("Tablo1")

    With rs
        For n = 0 To .Fields.Count - 1
            sResult = sResult & .Fields(n).Name & vbCrLf
        Next 'n
        .Close
    End With

    Set rs = Nothing

MsgBox sResult
Cevapla
#5
Bir incele kodları.Referenceden Microsoft.scripting.runtime yi işaretle



Function alanAdlar() As Dictionary

    Dim rs As DAO.Recordset
    Dim n As Long
    Dim scr As New Scripting.Dictionary

    Set rs = CurrentDb.OpenRecordset("Tablo1")
    With rs
        For n = 0 To .Fields.Count - 1
            scr(.Fields(n).name) = scr(.Fields(n).name)
        Next 'n
        .Close
    End With

  Set alanAdlar = scr
  Set rs = Nothing

End Function


Sub test()

    Dim rs As DAO.Recordset
    Dim n As Long


    Const strSQLCreateFoo_c As String = _
          "CREATE TABLE Foo" & _
          "(" & _
          "MyField1 INTEGER," & _
          "MyField2 Text(10)" & _
          ");"

    Set rs = CurrentDb.OpenRecordset("Foo")

    If Not TableExists("foo") Then
        CurrentDb.Execute strSQLCreateFoo_c
    End If
   
    For n = 0 To CurrentDb.OpenRecordset("Foo").Fields.Count - 1
        For Each xx In alanAdlar.Keys
          If rs.Fields(n).name = xx Then
                MsgBox "Bulunan Tablo ad: " & rs.Fields(n).name
          End If
        Next
    Next
    rs.Close
    Set rs = Nothing
'    CurrentDb.Execute strSQLAppendBs_c
End Sub



Private Function TableExists(ByVal name As String) As Boolean
    On Error Resume Next
    TableExists = LenB(CurrentDb.TableDefs(name).name)
End Function
Cevapla
#6
scr(.Fields(n).name) = scr(.Fields(n).name)
bu satır doğru mu
Cevapla

Bir hesap oluşturun veya yorum yapmak için giriş yapın

Yorum yapmak için üye olmanız gerekiyor

ya da
Task