Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
Solution: Adding byte() data to access DB from VB front end

 
Post new topic   Reply to topic    dna88 Forum Index -> Databases Discussion Forum
Author Message
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Solution: Adding byte() data to access DB from VB front end Reply with quote

I thought I put some light on this issue by throughing a piece of code. According to this project you can add pictures to your Access database field as binary, retrive it, display and modified from end user environment. Before I continue I like to put some lights on Access. It's not a RDBMS, DBMS. It has limited file size option. A .MDB file can
be up to 2GB. So, I recommend not to add pictures in to table. As you can store pictures in different folder and store the file location. In this way, you can save a lot fo space. Though it's necessary to store pictures in to table sometimes. I used VB to design the front end. You can use VC++ or Dephi.

Create a database:Employees.mdb
Table:Employees
Fields: Type Size
employeeID text 6
firstName text 15
lastName text 15
middleName text 15
address text 100
phone text 15
mobile text 15
eMail text 25
picture OLEobject

Use VB6 to create a project:prjPictures
Add Micro$oft activeX dataobject library 2.1
Add a form:frmEmployees
Add a module:modConn
****************************************
Add following code to "modConn":
Code:

Option Explicit

Public cnn As ADODB.Connection 'adodb connection variable

Public rsEmployees As ADODB.Recordset 'Table:Employees

Public Sub Init()
Dim str As String   'Connection string

Set cnn = New ADODB.Connection

Set rsEmployees = New ADODB.Recordset

'connected to the databse through connection string
str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Employees.mdb;Persist Security Info=False;"
cnn.ConnectionString = str
cnn.Open

rsEmployees.Open "Employees", cnn, adOpenDynamic, adLockOptimistic, adCmdTable

End Sub



****************************************
Add following Label in the form:
Name Caption
label1 Employee ID
label2 First Name
label3 Last Name
label4 Middle Name
label5 Address
label6 Phone
label7 Mobile
label8 E-mail
label9 Picture
---------------------------------
Add following label, image control and text box:
Name Caption
lblEmpID none
txtFirstName none
txtLastName none
txtMiddleName none
txtAddress none
txtPhone none
txtMobile none
txtEmail none
imgEmp
---------------------------------
Add following Menu:
File(mnuFile)
...Exit(subMnuExit) ctrl+X
Records(mnuRecords)
...Add Record(subMnuAdd) ctrl+A
...Delete Record(subMnuDelete) ctrl+D
Display(mnuDisplay)
...First Record(subMnuFirst) ctrl+F
...Next Record(subMnuNext) ctrl+N
...Previous Record(subMnuPrevious) ctrl+P
...Last Record(subMnuLast) ctrl+L
-----------------------------------
Add Micro$oft common dialog control component
-----------------------------------
Add following code to "frmEmployees":
Code:

Option Explicit
'Create By: Dinangkur Kundu(Ringku)
'Katnarpara, Bogra 5800, Bangladesh
'Date: 8th May, 2004
'url: www.geocities.com/lee13uk2000/
'
'control variable
Public ctrl As Control

Private Sub Form_Load()
'open database for data manipulation
Init
'Display new employee id
New_EmployeeID
'Set empty image box visibility to false
imgEmp.Visible = False
End Sub

Private Sub subMnuAdd_Click()
Dim byteData() As Byte

On Error GoTo eh

With dlgAdd
    .Filter = "Picture Files (*.jpg, *.gif)|*.jpg;*.gif"
    .DialogTitle = "Select Employee Picture to Add to Database"
    .ShowOpen
   
    'Open the picture file
    Open .FileName For Binary As #1
    ReDim byteData(FileLen(.FileName))
   
    'read the data and close the file
    Get #1, , byteData
    Close #1
End With

'Add record
With rsEmployees
    .AddNew
    .Fields("employeeID") = lblEmpID.Caption
    .Fields("firstName") = txtFirstName.Text
    .Fields("lastName") = txtLastName.Text
    .Fields("middleName") = txtMiddleName.Text
    .Fields("address") = txtAddress.Text
    .Fields("phone") = txtPhone.Text
    .Fields("mobile") = txtMobile.Text
    .Fields("eMail") = txtEmail.Text
    .Fields("picture").AppendChunk byteData
    .Update
End With
Clear_NewEntry
Exit Sub

eh:
If Err.Number = 32755 Then
      'user cancelled
    Else
      MsgBox Err.Description
      Err.Clear
      Clear_NewEntry
      Exit Sub
End If
End Sub

Private Sub subMnuClear_Click()
Clear_NewEntry
End Sub

Private Sub subMnuDelete_Click()
If Not (rsEmployees.BOF And rsEmployees.EOF) Then
    rsEmployees.Delete
    Clear_NewEntry
End If
End Sub

Private Sub subMnuExit_Click()
Unload Me
Set frmEmployees = Nothing
End Sub

Private Sub subMnuFirst_Click()
rsEmployees.MoveFirst
Display_Records
End Sub

Private Sub subMnuLast_Click()
rsEmployees.MoveLast
Display_Records
End Sub

Private Sub subMnuNext_Click()
rsEmployees.MoveNext
If rsEmployees.EOF Then rsEmployees.MoveLast
Display_Records
End Sub

Private Sub subMnuPrevious_Click()
rsEmployees.MovePrevious
If rsEmployees.BOF Then rsEmployees.MoveFirst
Display_Records
End Sub

Private Sub txtMobile_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 45 Then
    Exit Sub
End If
If IsNumeric(VBA.Chr(KeyAscii)) = False Then
    KeyAscii = 0
End If
End Sub

Private Sub txtPhone_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 45 Then
    Exit Sub
End If
If IsNumeric(VBA.Chr(KeyAscii)) = False Then
    KeyAscii = 0
End If
End Sub

Public Sub New_EmployeeID()
'generate employee id
Dim str As String
Dim s As String
Dim n As Integer

If (rsEmployees.BOF And rsEmployees.EOF) Then
    str = "EMP001"
Else
    rsEmployees.MoveLast
    str = rsEmployees("employeeID")
    s = VBA.Mid(str, 4, 3)
    n = VBA.Val(s)
    n = n + 1
    If n < 10 Then
        str = "EMP" & "00" & n
    ElseIf n >= 10 And n < 100 Then
        str = "EMP" & "0" & n
    Else
        str = "EMP" & n
    End If
End If
lblEmpID.Caption = str
End Sub

Public Sub Clear_NewEntry()
New_EmployeeID
imgEmp.Visible = False

For Each ctrl In Screen.ActiveForm.Controls
    If TypeOf ctrl Is TextBox Then
        ctrl = ""
        ctrl.SetFocus
    End If
Next
End Sub

Public Sub Display_Records()
'display record
If Not (rsEmployees.BOF And rsEmployees.EOF) Then
    imgEmp.Visible = True
    lblEmpID.Caption = rsEmployees.Fields("employeeID")
    txtFirstName.Text = rsEmployees.Fields("firstName")
    txtLastName.Text = rsEmployees.Fields("lastName")
    txtMiddleName.Text = rsEmployees.Fields("middleName")
    txtAddress.Text = rsEmployees.Fields("address")
    txtPhone.Text = rsEmployees.Fields("phone")
    txtMobile.Text = rsEmployees.Fields("mobile")
    txtEmail.Text = rsEmployees.Fields("eMail")
    Set imgEmp.DataSource = rsEmployees
    imgEmp.DataField = "picture"
End If
End Sub


Happy Hacking.

If you want to need the project mail me to get the .zip version.

-DK.
_________________
...we too are stardust...
Sat May 08, 04 4:37 am
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Pavel
Beginner User
Beginner User


Joined: 07 May 2004
Posts: 11

Post Post subject: Reply with quote

Hi DK

Your code is readable and easy. I tried to create project as u described. But I have faced following problems:
1. MenuItems are not working, (though I have seen that you didn't include any code for them). Then what is the purpose of creating Menu Bar.
2. Secondly, after running the application, I couldn't find any way to insert or load my picture.

Hope you don't mind to clear the things out.

---------
pavel
Sun May 16, 04 1:51 pm
Back to top
Pavel View user's profile Send private message
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Dear Pavel,

I checked with my code is working fine. I think you made a problem while creating menu by menu editior.

When the program load, put all the data then click on menuAdd button, a box will apear and asked for a location of the picture.

I hope that's help. If you want to get source code, send me a mail.

-DK.
_________________
...we too are stardust...
Sun May 16, 04 9:59 pm
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
dinangkur
Super Moderator
Super Moderator


Joined: 24 Mar 2004
Posts: 491
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Pavel,

what do you mean by menu code? Check with the procedure such as subMnuAdd() that the button of menu, under that there are codes.

-DK.
_________________
...we too are stardust...
Sun May 16, 04 11:26 pm
Back to top
dinangkur View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Databases Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group