HTML Source EditorWord Wrap
In this introduction to Microsoft Access Visual Basic for Applications programming, you will learn how to write your first code in Access VBA. Visual Basic 6.0 Examples. Web API Categories ASN.1 Amazon EC2 Amazon Glacier Amazon S3 Amazon S3 (new) Amazon SES Amazon SNS Amazon SQS Async Azure Cloud Storage Azure Service Bus Azure Table Service Base64 Bounced Email Box CAdES CSR CSV Certificates Compression DKIM / DomainKey DSA Diffie-Hellman Digital Signatures Dropbox. Apr 21, 2017 - Microsoft Access business database design and consulting. Web based programmers offering expert quoted solutions for database creation to match your requirements.
Before we write the code that connects our visual basic program to ms access, we will first add the Microsoft ADO 2.8 library in our visual basic. To add the Microsoft ADO 2.8 library, in the menu bar select Project and select References. In this example, let us access data in a DataGridView control using code. Take the following steps − Add a DataGridView control and a button in the form. Change the text of the button control to 'Fill'. Double click the button control to add the required code for the Click event of the button, as shown below −.
In visual basic, Access Modifiers are the keywords and those are useful to define an accessibility level for all the types and type members.
By specifying the access level for all the types and type members, we can control whether they can be accessed in other classes or in current assembly or in other assemblies based on our requirements.
The following are the different types of access modifiers available in a visual basic programming language.
- Friend (internal in c#)
By using these four access modifiers, we can specify a following six levels of accessibility for all types and type members based on our requirements.
Access Modifier | Description |
---|---|
Public | It is used to specifies that access is not restricted. |
Private | It is used to specifies that access is limited to the containing type. |
Protected | It is used to specifies that the access is limited to the containing type or types derived from the containing class. |
Friend (internal) | It is used to specifies that access is limited to the current assembly. |
Protected Friend | It is used to specifies that access is limited to the current assembly or types derived from the containing class. |
Generally, in visual basic only one access modifier is allowed to use with any member or type, except when we use Protected Friend combination.
In visual basic, we are not allowed to use any access modifiers on namespaces, because the namespaces have no access restrictions.
Only certain access modifiers are allowed to specify based on the context in which the member declaration occurs. In case, if we didn’t mention any access modifiers during the member declaration, then the default access modifiers will be used depending on the member declaration context.
For example, the top-level types which are not nested in any other types can only have Public
or Friend
accessibility. The default accessibility for top-level types is Friend
.
Visual Basic Public Access Modifier
In visual basic, Public
modifier is useful to specify that access is not restricted, so the defined type or member can be accessed by any other code in the current assembly or another assembly that references it.
Following is the example of defining members with Public
modifier in a visual basic programming language.
Module Module1
ClassUser
Public Name AsString
Public Location AsString
Public Age AsInteger
PublicSub GetUserDetails()
Console.WriteLine('Name: {0}', Name)
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Sub Main()
Dim u AsUser = NewUser()
u.Name = 'Suresh Dasari'
u.Location = 'Hyderabad'
u.Age = 32
u.GetUserDetails()
Console.WriteLine(vbLf & 'Press Enter Key to Exit.')
Console.ReadLine()
EndSub
EndModule
If you observe the above example, we defined a Userclass with required variables and method using Public
access modifier and trying to access those variables and method in another class with an object reference of Userclass.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, we are able to access the variables and methods of Userclass in another class because of specifying with Public
specifier based on our requirements.
As discussed, the Public
access specifier will make all the defined members or types available to all the types in our application.
Visual Basic Private Access Modifier
In visual basic, Private
modifier is useful to specify that access is limited to the containing type so the defined type or member can only be accessed by the code in the same class or structure.
Following is the example of defining members with Private
modifier in a visual basic programming language.
Module Module1
ClassUser
Private Name AsString
Private Location AsString
Private Age AsInteger
PrivateSub GetUserDetails()
Console.WriteLine('Name: {0}', Name)
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Sub Main()
Dim u AsUser = NewUser()
' Complie-time Error
' These are inaccessible due to private specifier
u.Name = 'Suresh Dasari'
u.Location = 'Hyderabad'
u.Age = 32
u.GetUserDetails()
Console.WriteLine(vbLf & 'Press Enter Key to Exit.')
Console.ReadLine()
EndSub
EndModule
If you observe the above example, we defined a Userclass with required variables and method using Private
access modifier and trying to access those variables and method in another class with an object reference of Userclass.
When we execute the above visual basic program, we will get compile-time errors like as shown below.
If you observe the above result, we got the compile-time error because the Private
modifier members of Userclass referred in another class.
As discussed, the Private
modifier type or member can be accessed only by the code within the same class or structure.
Visual Basic Protected Access Modifier
In visual basic, Protected
modifier is useful to specify that access is limited to the containing type or types derived from the containing class so the type or member can only be accessed by code within the same class or in a derived class.
Following is the example of defining members with Protected
modifier in a visual basic programming language.
Module Module1
ClassUser
Protected Name AsString
Protected Location AsString
Protected Age AsInteger
ProtectedSub GetUserDetails()
Console.WriteLine('Name: {0}', Name)
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Sub Main()
Dim u AsUser = NewUser()
' Complie-time Error
' These are inaccessible due to protected specifier
u.Name = 'Suresh Dasari'
u.Location = 'Hyderabad'
u.Age = 32
u.GetUserDetails()
Console.WriteLine(vbLf & 'Press Enter Key to Exit.')
Console.ReadLine()
EndSub
EndModule
If you observe the above example, we defined a Userclass with required variables and method using Protected
access modifier and trying to access those variables and method in another class with an object reference of Userclass.
When we execute the above visual basic program, we will get compile-time errors like as shown below.
If you observe the above result, we got a compile-time error because the Protected
modifier members of Userclass referred in another class.
As discussed, the Protected
members of a base class can be accessible in a derived class, only when access occurs through the derived class type.
Following is the example of accessing a base classProtected
members in derived class through derived class type.
Class User
Protected Name AsString
Protected Location AsString
Protected Age AsInteger
ProtectedSub GetUserDetails()
Console.WriteLine('Name: {0}', Name) Tx studio viewer.
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Class Details
InheritsUser
PublicSharedSub Main()
Dim u AsUser = NewUser()
Dim d AsDetails = NewDetails()
' Complier Error
' protected members can only accessible with derived classes
' u.Name = 'Suresh Dasari';
d.Name = 'Suresh Dasari'
Visual Basic Code Examples For Access Point
d.Location = 'Hyderabad'
d.Age = 32
d.GetUserDetails()
Console.WriteLine(vbLf & 'Press Enter Key to Exit.')
Console.ReadLine()
Rcon tool. EndSub
EndClass
If you observe the above example, we are accessing base class (User) Protected
members using the reference of derived class (Details) and if we uncomment the commented code we will get a compile-time error because we are trying to access the protected members with base class (User) reference instead of derived class (Details).
When we execute the above visual basic program, we will get the result as shown below.
This is how we can use Protected
modifier in our visual basic applications to limit access of type or member in the same class or derived class based on our requirements.
Protected
because the structure cannot be inherited.Visual Basic Friend Access Modifier
In visual basic, Friend
modifier is useful to specify that access is limited to the current assembly so the type or member can be accessed by any code in the same assembly, but not from another assembly.
Following is the example of defining members with Friend
modifier in a visual basic programming language.
Module Module1
ClassUser
Friend Name AsString
Friend Location AsString
Friend Age AsInteger
FriendSub GetUserDetails()
Console.WriteLine('Name: {0}', Name)
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Sub Main()
Dim u AsUser = NewUser()
u.Name = 'Suresh Dasari'
u.Name = 'Suresh Dasari'
u.Location = 'Hyderabad'
u.Age = 32
u.GetUserDetails()
Console.WriteLine(vbLf & 'Press Enter Key to Exit.')
Console.ReadLine()
EndSub
EndModule
If you observe the above example, we defined a Userclass with required variables and method using Friend
access modifier and trying to access those variables and method in another class with an object reference of Userclass.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, we are able to access the variables and methods of Userclass in another class because of specifying with Friend
specifier based on our requirements.
As discussed, in visual basic the Friend
type or members are accessible within the files of the same assembly.
Visual Basic Protected Friend Access Modifier
In visual basic, the Protected Friend
modifier is useful to specify that access is limited to the current assembly or types derived from the containing class. So, the type or member can be accessed by any code in the same assembly or by any derived class in another assembly.
Following is the example of defining members with Protected Friend
modifier in a visual basic programming language.
Module Module1
ClassUser
ProtectedFriend Name AsString
ProtectedFriend Location AsString
ProtectedFriend Age AsInteger
ProtectedFriendSub GetUserDetails()
Console.WriteLine('Name: {0}', Name)
Console.WriteLine('Location: {0}', Location)
Console.WriteLine('Age: {0}', Age)
EndSub
EndClass
Sub Main()
Dim u AsUser = NewUser()
u.Name = 'Suresh Dasari'
u.Name = 'Suresh Dasari'
u.Location = 'Hyderabad'
u.Age = 32
u.GetUserDetails()
Console.WriteLine('Press Enter Key to Exit.')
Console.ReadLine()
EndSub
EndModule
If you observe the above example, we defined a Userclass with required variables and method using Protected Friend
access modifier and trying to access those variables and method in another class with an object reference of Userclass.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, we are able to access the variables and methods of Userclass in another class because of specifying with Friend
specifier based on our requirements.
As discussed, in visual basic the Protected Friend
type or members are accessible from the current assembly or from the types that are derived from the containing class in another assembly.
- VB.Net Basic Tutorial
- VB.Net Advanced Tutorial
- VB.Net Useful Resources
- Selected Reading
Applications communicate with a database, firstly, to retrieve the data stored there and present it in a user-friendly way, and secondly, to update the database by inserting, modifying and deleting data.
Sample Visual Basic Program Code
Microsoft ActiveX Data Objects.Net (ADO.Net) is a model, a part of the .Net framework that is used by the .Net applications for retrieving, accessing and updating data.
ADO.Net Object Model
ADO.Net object model is nothing but the structured process flow through various components. The object model can be pictorially described as −
The data residing in a data store or database is retrieved through the data provider. Various components of the data provider retrieve data for the application and update data.
An application accesses data either through a dataset or a data reader.
Datasets store data in a disconnected cache and the application retrieves data from it.
Data readers provide data to the application in a read-only and forward-only mode.
Data Provider
A data provider is used for connecting to a database, executing commands and retrieving data, storing it in a dataset, reading the retrieved data and updating the database.
The data provider in ADO.Net consists of the following four objects −
Sr.No. | Objects & Description |
---|---|
1 | Connection This component is used to set up a connection with a data source. |
2 | Command A command is a SQL statement or a stored procedure used to retrieve, insert, delete or modify data in a data source. |
3 | DataReader Data reader is used to retrieve data from a data source in a read-only and forward-only mode. |
4 | DataAdapter This is integral to the working of ADO.Net since data is transferred to and from a database through a data adapter. It retrieves data from a database into a dataset and updates the database. When changes are made to the dataset, the changes in the database are actually done by the data adapter. |
There are following different types of data providers included in ADO.Net
The .Net Framework data provider for SQL Server - provides access to Microsoft SQL Server.
The .Net Framework data provider for OLE DB - provides access to data sources exposed by using OLE DB.
The .Net Framework data provider for ODBC - provides access to data sources exposed by ODBC.
The .Net Framework data provider for Oracle - provides access to Oracle data source.
The EntityClient provider - enables accessing data through Entity Data Model (EDM) applications.
DataSet
DataSet is an in-memory representation of data. It is a disconnected, cached set of records that are retrieved from a database. When a connection is established with the database, the data adapter creates a dataset and stores data in it. After the data is retrieved and stored in a dataset, the connection with the database is closed. This is called the 'disconnected architecture'. The dataset works as a virtual database containing tables, rows, and columns.
The following diagram shows the dataset object model −
The DataSet class is present in the System.Data namespace. The following table describes all the components of DataSet −
Sr.No. | Components & Description |
---|---|
1 | DataTableCollection It contains all the tables retrieved from the data source. |
2 | DataRelationCollection It contains relationships and the links between tables in a data set. |
3 | ExtendedProperties It contains additional information, like the SQL statement for retrieving data, time of retrieval, etc. |
4 | DataTable It represents a table in the DataTableCollection of a dataset. It consists of the DataRow and DataColumn objects. The DataTable objects are case-sensitive. |
5 | DataRelation It represents a relationship in the DataRelationshipCollection of the dataset. It is used to relate two DataTable objects to each other through the DataColumn objects. |
6 | DataRowCollection It contains all the rows in a DataTable. |
7 | DataView It represents a fixed customized view of a DataTable for sorting, filtering, searching, editing and navigation. |
8 | PrimaryKey It represents the column that uniquely identifies a row in a DataTable. |
9 | DataRow It represents a row in the DataTable. The DataRow object and its properties and methods are used to retrieve, evaluate, insert, delete, and update values in the DataTable. The NewRow method is used to create a new row and the Add method adds a row to the table. |
10 | DataColumnCollection It represents all the columns in a DataTable. |
11 | DataColumn It consists of the number of columns that comprise a DataTable. |
Connecting to a Database
The .Net Framework provides two types of Connection classes −
SqlConnection − designed for connecting to Microsoft SQL Server.
OleDbConnection − designed for connecting to a wide range of databases, like Microsoft Access and Oracle.
Example 1
We have a table stored in Microsoft SQL Server, named Customers, in a database named testDB. Please consult 'SQL Server' tutorial for creating databases and database tables in SQL Server.
Let us connect to this database. Take the following steps −
Select TOOLS → Connect to Database
Select a server name and the database name in the Add Connection dialog box.
M
Click on the Test Connection button to check if the connection succeeded.
Add a DataGridView on the form.
Click on the Choose Data Source combo box.
Click on the Add Project Data Source link.
This opens the Data Source Configuration Wizard.
Select Database as the data source type
Choose DataSet as the database model.
Choose the connection already set up.
Save the connection string.
Choose the database object, Customers table in our example, and click the Finish button.
Select the Preview Data link to see the data in the Results grid −
When the application is run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Example 2
In this example, let us access data in a DataGridView control using code. Take the following steps −
Add a DataGridView control and a button in the form.
Change the text of the button control to 'Fill'.
Double click the button control to add the required code for the Click event of the button, as shown below −
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Clicking the Fill button displays the table on the data grid view control −
Creating Table, Columns and Rows
We have discussed that the DataSet components like DataTable, DataColumn and DataRow allow us to create tables, columns and rows, respectively.
The following example demonstrates the concept −
Example 3
So far, we have used tables and databases already existing in our computer. In this example, we will create a table, add columns, rows and data into it and display the table using a DataGridView object.
Take the following steps −
Add a DataGridView control and a button in the form.
Change the text of the button control to 'Fill'.
Add the following code in the code editor.
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Clicking the Fill button displays the table on the data grid view control −