Access VBA Macro Programming

(Joao Candeias) #1

then you can use a message box to ask the user for a confirmation to save. If the user answers
No, then the code uses the Undo method and all changes on the form are undone.


Me.Undo


Visible


You can use the Visible property to hide the form or report without closing it when it is not in
use. The settings are True (visible) and False (not visible).


Me.Visible = False


This code will hide the form.


CurrentDb Object


TheCurrentDbobject represents the current databases in terms of tables, queries, and
recordsets. It is used in VBA for communicating with the tables and queries.


Main Properties, Methods, and Collections


This section details the main properties, methods, and collections you will use within the
CurrentDbobject.


CreateQueryDef


TheCreateQueryDefmethod creates a new query based on a name and a SQL text string:


CurrentDb.CreateQueryDef "NewQuery", "select * from MyTable"


This will create a new query called NewQuery, which will have the SQL “select * from
MyTable.”


CreateTableDef


TheCreateTableDefmethod is used to create a new table definition. However, unlike the
CreateQueryDefmethod, it needs further code to create the field definitions and to append
the new table definition to the database:


Sub CreateTable()
Dim MyTable As TableDef


Set MyTable = CurrentDb.CreateTableDef("NewTable")


MyTable.Fields.Append MyTable.CreateField("LastName", dbText, 100 )


CurrentDb.TableDefs.Append MyTable


End Sub


Chapter 15: The Main Objects 205

Free download pdf