What's new

Closed Vb6 and mysql basic database connection (with additional simple crud project)

Status
Not open for further replies.

stultusrex

Addict
Joined
Aug 12, 2016
Posts
109
Reaction
21
Points
88
Hello guys,

may nakita akong mga threads dito na nanghihingi ng sample kung pano mag connect sa MySQL database using VB6. This procedure can also be used in VB.NET, but of course there will be some small differences on integrating them.

First things first:
You need to download the ff.

1.) You do not have permission to view the full content of this post. Log in or register now.
2.) You do not have permission to view the full content of this post. Log in or register now.
- I recommend downloading the offline installer
- alternatively you can also use WAMP or XAMPP because their package also includes MySQL database, plus you can work with web stuff if you're into it.

3.) Install them according to your needs, installing them in express or default settings should work just fine. Just remember your MySQL User and Password during installation cause you'll need that later on.

4.) Once your MySQL Server is up and running you can now create your database using the shell commands, yes, shell commands. Meaning the black command prompt. Learning the basics is not that bad.

5.) But of course there's always an easy way to do that. MySQL offers a free database manager which allows you to, well, manage your database. It's called You do not have permission to view the full content of this post. Log in or register now.. Create database, tables, etc. it's all in there. It's just a matter of exploring. There are other alternative software out there like the commonly used SQLYog.

Once that's all set and done, you can now start your VB6 Project

6.) Open and create a new Project in VB6
7.) You need to add a reference library to your project. To do that go to Project menu and click on References
1-29-2017 5-32-21 PM.png

8.) Next, find and check Microsoft ActiveX Data Objects Library (select the latest version possible)
1-29-2017 5-44-41 PM.png

9.) Under Project menu select Add Module
10.) In this module declare a variable that will hold your connection

Sample:
Public Conn As ADODB.Connection

11.) Now create a function that will try to connect to your MySQL server database:
This function will return a value of True or False which determines whether its connection was successful or not.

*Always remember that server, database, user, and password may vary depending on your setup.

Sample function code:

Function MySQLConnection() As Boolean
On Error GoTo Errz
Set Conn = New ADODB.Connection
Conn.CursorLocation = adUseClient
Conn.Open "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=sample_db;User=stultusrex;Password=1234;Option=3;"
MySQLConnection = True
Exit Function
Errz:
MsgBox Err.Number & vbCr & Err.Description, vbCritical
Err.Clear
MySQLConnection = False
Exit Function
End Function


Let's see how this works.

• As you can see the driver I used in this example is MySQL ODBC 3.51 Driver, this is an older version of MySQL ODBC. For you to find out what your current ODBC version is go to Control Panel>Administrative Tools>Data Sources (ODBC)>Drivers> and search for the MySQL ODBC

1-29-2017 4-14-08 PM.png

12.) Now in your Main Form (or Sub Main), call the function MySQLConnection

Sample:

Private Sub Form_Load()

If MySQLConnection = False Then End

End Sub


This means that once your Main Form loads, it will try to connect to your database. If the function returns false the project will End from execution. Of course you can improvise this idea by sending the user a message box that will prompt him a connection failure or methods you have.

That's basically it. If your project proceeds on loading that means you're connected to your MySQL Database.

I added a sample and a simple project to help you out with your progress in this. The coding is not that fancy since I just coded that for like what 30-40 mins, just to give you an idea on how it works.;)
The project includes adding, retrieving, editing, and deleting of records from your database.

Leave a comment if you have questions.
Have nice programming.




P.S.

This is my first thread, sorry if it's all messed up.
 

Attachments

Status
Not open for further replies.
Back
Top