|
| | | |
• I.2. Access - Tabele i kwerendy [1] •
- 2.1 Jak policzyć rekordy w tabeli ?
- 2.2 Jak sprawdzić, czy istnieje tabela ?
- 2.3 Jak sprawdzić, czy istnieje pole w tabeli ?
- 2.4 Jak dodać pole do tabeli bazowej lub tabeli połączonej ?
- 2.5 Jak zmienić nazwę pola w tabeli bazowej lub tabeli połączonej ?
- 2.6 Jak wywołując funkcję DLookup przekazać kwerendzie parametr ?
- 2.7 Dlaczego kwerenda wywołuje => Błąd wykonania '3061' Za mało parametrów. Oczekiwano np. 2 ?
- 2.8 Czy można odzyskać usuniętą przez pomyłkę tabelę ?
- 2.9 Jak obliczyć średnią liczb z 2 lub więcej pól tego samego rekordu ?
- 2.10 Jak ukryć tabelę, by nie była widoczna w oknie bazy (nawet po ustawieniu w menu Narzędzia/Opcje/Widok/Pokaż obiekty ukryte(systemowe) = True ?
- 2.11 Jak pobrać współrzędne kursora myszy nad oknem tabeli (kwerendy) poza obszarem danych ?
- 2.12 Jak sprawdzić czy zdarzenie MouseUp kwerendy, zainicjowane zdarzeniem MouseDown nad nagłówkiem kolumny lub selektorem rekordów, zaszło nad obszarem danych ?
- 2.13 Jak sprawdzić, czy tabela lub kwerenda otwarta jest w widoku Arkusz danych (DataSheet), a formularz lub raport nie jest w widoku Projekt (DesignView) ?
| | | | |
|
| | |
|
2.4 Jak dodać pole do tabeli bazowej lub tabeli połączonej ?

' dodaje nowe pole do tabeli, przy powodzeniu zwraca ciąg zerowej długości, przy błędzie zwraca ciąg znaków będący krótkim opisem błędu,
Public Function knAddField(sTableName As String, _
sFieldName As String, _
lFieldType As Long) As String
Dim dbs As DAO.Database
Dim CurDbs As DAO.Database
Dim tdf As DAO.TableDef
Dim CurTdf As DAO.TableDef
Dim fld As DAO.Field
Dim sPath As String
Dim sPsw As String
Dim lInStart As Long
Dim lInEnd As Long
Const MY_PWD As String = "PWD="
Const MY_DBS As String = "DATABASE="
Set CurDbs = CurrentDb
' sprawdź, czy istnieje tabela
If knTableExists(sTableName) = False Then
knAddField = "Tabela [" & sTableName & "] nie istnieje !"
Set CurDbs = Nothing
Exit Function
End If
Set CurTdf = CurDbs.TableDefs(sTableName)
With CurTdf
If Len(.Connect) > 0 Then
' szukaj hasła bazy z której pochodzi połączona tabela
lInStart = InStr(1, .Connect, MY_PWD, vbBinaryCompare)
If lInStart > 0 Then
lInEnd = InStr(lInStart + Len(MY_PWD), _
.Connect, ";", vbBinaryCompare)
sPsw = Mid$(.Connect, lInStart + Len(MY_PWD), _
lInEnd - (lInStart + Len(MY_PWD)))
End If
' szukaj ścieżki bazy z której pochodzi połączona tabela
lInStart = InStr(1, .Connect, MY_DBS, vbBinaryCompare)
If lInStart > 0 Then
sPath = Mid$(.Connect, lInStart + Len(MY_DBS))
Else
knAddField = "Brak ścieżki bazy !"
Set CurTdf = Nothing
Set CurDbs = Nothing
Exit Function
End If
Set dbs = OpenDatabase(sPath, False, False, ";PWD=" & sPsw)
Set tdf = dbs.TableDefs(.SourceTableName)
Else
Set tdf = CurTdf
End If
End With
If Not knFieldExists(sTableName, sFieldName) Then
Set fld = tdf.CreateField(sFieldName, lFieldType)
tdf.Fields.Append fld
tdf.Fields.Refresh
Else
knAddField = "Pole [" & sFieldName & "] już istnieje."
End If
Set fld = Nothing
Set CurTdf = Nothing
Set CurDbs = Nothing
Set dbs = Nothing
End Function

' przykładowe wywołanie:
Private Sub btnTest_Click()
Dim sRet As String
' dodaj do tabeli: "Tabela1" pole typu: Long Integer o nazwie: "NowePole"
sRet = knAddField("Tabela1", "NowePole1", dbLong)
If Len(sRet) = 0 Then
sRet = "Pole dodano."
End If
MsgBox sRet
End Sub
ΔΔΔ | | | | |
|
| | |
|
2.5 Jak zmienić nazwę pola w tabeli bazowej lub tabeli połączonej ?

' zmienia nazwę pola do tabeli, przy powodzeniu zwraca ciąg zerowej długości, przy błędzie zwraca ciąg znaków będący krótkim opisem błędu,
Public Function knRenameField(sTableName As String, _
sOldName As String, _
sNewName As String) As String
Dim dbs As DAO.Database
Dim CurDbs As DAO.Database
Dim tdf As DAO.TableDef
Dim CurTdf As DAO.TableDef
Dim fld As DAO.Field
Dim sPath As String
Dim sPsw As String
Dim lInStart As Long
Dim lInEnd As Long
Const MY_PWD As String = "PWD="
Const MY_DBS As String = "DATABASE="
Set CurDbs = CurrentDb
' sprawdź, czy istnieje tabela
If knTableExists(sTableName) = False Then
knRenameField = "Tabela [" & sTableName & "] nie istnieje !"
Set CurDbs = Nothing
Exit Function
End If
Set CurTdf = CurDbs.TableDefs(sTableName)
With CurTdf
If Len(.Connect) > 0 Then
' szukaj hasła bazy z której pochodzi połączona tabela
lInStart = InStr(1, .Connect, MY_PWD, vbBinaryCompare)
If lInStart > 0 Then
lInEnd = InStr(lInStart + Len(MY_PWD), _
.Connect, ";", vbBinaryCompare)
sPsw = Mid$(.Connect, lInStart + Len(MY_PWD), _
lInEnd - (lInStart + Len(MY_PWD)))
End If
' szukaj ścieżki bazy z której pochodzi połączona tabela
lInStart = InStr(1, .Connect, MY_DBS, vbBinaryCompare)
If lInStart > 0 Then
sPath = Mid$(.Connect, lInStart + Len(MY_DBS))
Else
knRenameField = "Brak ścieżki bazy !"
Set CurTdf = Nothing
Set CurDbs = Nothing
Exit Function
End If
Set dbs = OpenDatabase(sPath, False, False, _
";PWD=" & sPsw)
Set tdf = dbs.TableDefs(.SourceTableName)
Else
Set tdf = CurTdf
End If
End With
If knFieldExists(sTableName, sOldName) = True Then
If knFieldExists(sTableName, sNewName) = True Then
knRenameField = "Pole [" & sNewName & "] już istnieje."
Else
tdf.Fields(sOldName).Name = sNewName
End If
Else
knRenameField = "Pole [" & sOldName & "] nie istnieje."
End If
Set fld = Nothing
Set CurTdf = Nothing
Set CurDbs = Nothing
Set dbs = Nothing
End Function

' przykładowe wywołanie:
Private Sub btnTest_Click()
Dim sRet As String
' zmień w tabeli: "Tabela1" nazwę pola: "Pole1" na "NowaNazwa"
sRet = knRenameField("Tabela1", "Pole1", "NowaNazwa")
If Len(sRet) = 0 Then
sRet = "Nazwa pola została zmieniona."
End If
MsgBox sRet
End Sub
ΔΔΔ | | | | |
|
| |