Skip to content

Commit

Permalink
Add Access VBA Intro
Browse files Browse the repository at this point in the history
  • Loading branch information
areed1192 committed Dec 27, 2020
1 parent 8981fcf commit babb681
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions vba/vba-access/introduction.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Option Compare Database

Sub IntroductionToAccess()

'Declare our variables.
Dim accessApp As Application
Dim accessDatabase As Database
Dim accessTable As TableDef
Dim accessRecord As Recordset

'Grab the access application.
Set accessApp = Application

'Grab the Current Database in our application.
Set accessDatabase = accessApp.CurrentDb
Set accessDatabase = Application.DBEngine.Workspaces(0).Databases(0)

Debug.Print "The Database Full Path is:" + accessDatabase.Name
Debug.Print "The Number of tables in the database are: " + CStr(accessDatabase.TableDefs.Count)

accessDatabase.TableDefs.Delete Name:="StockPrices"

'Let's create a new table definition.
Set accessTable = accessDatabase.CreateTableDef(Name:="StockPrices")

'Take the new table and add some fields to it.
With accessTable

'Add a field for the Date.
.Fields.Append .CreateField("date", dbDate)

'Add a field for the open, close, high, low note that these are doubles.
.Fields.Append .CreateField("open", dbDouble)
.Fields.Append .CreateField("close", dbDouble)
.Fields.Append .CreateField("high", dbDouble)
.Fields.Append .CreateField("low", dbDouble)

End With

'Add the new table to the Table Definitions Collection.
accessDatabase.TableDefs.Append Object:=accessTable

'Refresh the Table Definitions Collection database,
accessDatabase.TableDefs.Refresh

'Once we add the table, we can add records to it. Let's open a new recordset object.
Set accessRecord = accessDatabase.OpenRecordset(Name:="StockPrices")

'Use the `addNew` method to add a new record.
accessRecord.AddNew

'Specify the different fields we defined up above.
accessRecord("date").Value = "12-26-2020"
accessRecord("open").Value = 12
accessRecord("close").Value = 12.2
accessRecord("high").Value = 12.3
accessRecord("low").Value = 11.99

'Update the Recordset.
accessRecord.Update

End Sub

0 comments on commit babb681

Please sign in to comment.