Minggu, 18 Desember 2011

Conection Visual Basic 6.0 My SQL 3.2.0.1

Langkah Pertama Buat database di SQL
create database latvb6csr;
= Table structure for table pendaftar

|------
|Field|Type|Null|Default
|------
|NO|int(11)|Yes|NULL
|NIK|varchar(16)|Yes|NULL
|NAMA|varchar(45)|Yes|NULL
|ALAMAT|varchar(50)|Yes|NULL
|RT|varchar(3)|Yes|NULL
|TTD|varchar(255)|Yes|NULL
|KETERANGAN|varchar(255)|Yes|NULL
|KELURAHAN|varchar(25)|Yes|NULL
|TANGGAL|datetime|Yes|NULL
|PETUGAS|varchar(25)|Yes|NULL
== Dumping data for table pendaftar


Buat Project baru dan disain form sebagai berikut





Buat Module isikan kode berikut
Public Conn As New ADODB.Connection
________________________________________
Private Sub buat_koneksi()
Dim ConnString As String
Dim db_name As String
Dim db_server As String
Dim db_port As String
Dim db_user As String
Dim db_pass As String
'//error traping
On Error GoTo buat_koneksi_Error
'/isi variable
db_name = FrmConection.TxtDbName 'nama database
db_server = FrmConection.TxtServer 'ganti jika server anda ada di komputer lain
db_port = FrmConection.TxtPort 'default port is 3306
db_user = FrmConection.TxtUser 'sebaiknya pakai username lain.
db_pass = FrmConection.TxtPassword 'password_anda
'/buat connection string
ConnString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & db_server & ";DATABASE=" & db_name & ";UID=" & db_user & ";PWD=" & db_pass & ";PORT=" & db_port & ";OPTION=3"
'/buka koneksi
With Conn
.ConnectionString = ConnString
.Open
End With


'___________________________________________________________
On Error GoTo 0
Exit Sub

buat_koneksi_Error:
MsgBox "Ada kesalahan dengan server, periksa apakah server sudah berjalan !", vbInformation, "Cek Server"
FrmConection.Show
End Sub
________________________________________
Sub Conect()
buat_koneksi
End Sub

Form Conection
Private Sub Command1_Click()
Timer1.Interval = 0
Unload FrmReport
Me.Hide
FrmReport.Show
End Sub
________________________________________
Private Sub Command2_Click()
Me.Hide
End Sub
________________________________________
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
________________________________________
Private Sub Timer1_Timer()
Me.Show
End Sub
________________________________________
Form 2
Option Explicit
Dim rsPendaftar As ADODB.Recordset
________________________________________
Private Sub Adodc1_WillMove(ByVal adReason As ADODB.EventReasonEnum, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
Adodc1.Caption = "Index = " & Adodc1.Recordset.AbsolutePosition & " Count = " & Adodc1.Recordset.RecordCount & " Source : " & Adodc1.Recordset.Source
End Sub
________________________________________
Private Sub Command1_Click()
End
End Sub
________________________________________


Private Sub DataGrid1_Click()
Adodc1.Caption = "Index = " & Adodc1.Recordset.AbsolutePosition & " Count = " & Adodc1.Recordset.RecordCount & " Source : " & Adodc1.Recordset.Source
End Sub
________________________________________
Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
Adodc1.Caption = "Index = " & Adodc1.Recordset.AbsolutePosition & " Count = " & Adodc1.Recordset.RecordCount & " Source : " & Adodc1.Recordset.Source
End Sub
________________________________________
Private Sub DataGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
Adodc1.Caption = "Index = " & Adodc1.Recordset.AbsolutePosition & " Count = " & Adodc1.Recordset.RecordCount & " Source : " & Adodc1.Recordset.Source
End Sub
________________________________________
Private Sub Form_Load()
Conect
Adodc1.ConnectionString = Conn
Adodc1.RecordSource = "Pendaftar"
Set DataGrid1.DataSource = Adodc1
'Adodc1.Caption = "Index = " & Adodc1.Recordset.CursorLocation & " Count = " & Adodc1.Recordset.RecordCount & " Source : " & Adodc1.Recordset.Source
End Sub
________________________________________
Private Sub Form_Resize()
On Error Resume Next
DataGrid1.Width = Me.Width - 500
DataGrid1.Height = Me.Height - 2545
Adodc1.Top = Me.Height - 900
Adodc1.Width = Me.Width - 500
End Sub
________________________________________
Private Sub Form_Unload(Cancel As Integer)
If Conn.State = adStateOpen Or Conn.State = adStateConnecting Then
Conn.Close
Set Conn = Nothing
End If
End Sub


Conectio SQL.rar

Tidak ada komentar:

Posting Komentar