VBA example - Money Exchange Program


Create money exchange program

In this example, you will learn how create Money Exchange program. The Money Exchange program will help you a lot in exchanging the money from Khmer (Riel) money to USD dollar and vice versa. The program provides you two languages: English and Khmer and the default language is English.  This program constructs by three labels,  a tab with two pages, threes text boxes and two command buttons. See the interface of program as figure below:

 VBA example - Microsoft Access money exchange program 



The Money Exchange program has the following functions: 

- If you want show Khmer language interface, click Khmer label. Alternatively, click English label if you want to switch to English.

- Khmer (Riel) tab is for exchanging money from USD dollar to Khmer (Riel) .

- Dollar tab is for exchanging money from Khmer currency to Dollar.

- Exchange Rate text box is for entering exchange rate in one dollar and amount of money you want to exchange will be input in the Amount text box.

- Exchange and Clear command buttons are clicked to exchange money and clear data in the two text boxes.

To exchange money, you must enter exchange rate and amount of money you want to exchange. For example, you want to exchange 5$ to Khmer(Riel). You use default language. Firstly, you must type exchange rate and amount into Exchange Rate and Amount text box. Then click Exchange button. You will see amount of Khmer(Riel) in the Total text box. 

You can create this program by following the steps below:

1. Open your Access database

2. Create a form design as figure below:

VBA example - Microsoft Access money exchange 

3. Drag and drop three labels, one tab with two pages, six text boxes (three text boxes for page1 and three text boxes for page2), and four command buttons(two command buttons for page1 and other two for page2) on the form. The figure above shows the controls that locate on page1 of tab control and page2 has the same controls as page1.

4. Set Name and Caption properties of the controls as following:

Control Name Caption
Label1 lblTitle Money Exchange
Label2 lblEn English
Label3 lblKh  Khmer 
Page1 Kh Khmer(Riel)
Label4 lblRateKh Exchage Rate 
Label5  lblAmountKh  Amount 
Label6  lblTotalKh  Total 
Text0 txtRateKh  
Text1 txtAmountKh  
Text2 txtTotalKh  
Command0  CmdExchangeKh  Exchange 
Command1  CmdClearKh  Clear 
Page2  Dol  Dollar 
Label7  lblRateDol  Exchang Rate 
Label8  lblAmontDol  Amount 
Label9  lblTotalDol  Total 
Text3  txtRateDol   
Text4  txtAmountDol   
Text5  txtTotalDol   
Command2  CmdExchangeDol Exchange 
Command3  CmdClearDol  Clear 

 5. Create two tables, one for storing Khmer language and the other for storing English as shown figure:

Access English table 

Access Khmer table 

After you finish steps 1 to 5, apply VBA code below: 

Option Compare Database
Option Explicit

Dim rs As Recordset
Dim db As Database

Private Sub Form_Load()
    txtTotal0.Enabled = False
    txtTotal1.Enabled = False
    switchtoen
End Sub

Private Sub lblen_Click()
    switchtoen
End Sub

Private Sub lblKh_Click()
     switchtokh
End Sub

Private Sub CmdClearKh_Click()
    txtExchangeRate0.Value = ""
    txtAmount0.Value = ""
    txtTotal0.Value = ""
End Sub

Private Sub CmdClearDol_Click()
    txtExchangeRate1.Value = ""
    txtAmount1.Value = ""
    txtTotal1.Value = ""
End Sub

Private Sub CmdExchange1_Click()
   On Error GoTo aa
    txtTotal1.Value = Val(txtAmount1.Value) / Val(txtExchangeRate1.Value)
    aa: MsgBox "Please enter value in Exchange rate or Amont text box"
End Sub

Private Sub CmdExhangeKh_Click()
  On Error GoTo aa
   txtTotal0.Value = Val(txtExchangeRate0.Value) * Val(txtAmount0.Value)
  aa: MsgBox "Please enter value in Exchange rate or Amont text box"
End Sub

Sub switchtoen()
   'Open current database
    Set db = CurrentDb
   'Open recordset
    Set rs = db.OpenRecordset("Select * FROM tblEn")
   'Assign value(read data from recordset)to controls
    lblTitle.Caption = rs(0).Value
    Kh.Caption = rs(1).Value
    Dol.Caption = rs(2).Value
    lblRateKh.Caption = rs(3).Value
    lblAmountKh.Caption = rs(4).Value
    lblTotalKh.Caption = rs(5).Value
    CmdExhangeKh.Caption = rs(6).Value
    CmdClearKh.Caption = rs(7).Value

    lblRateDol.Caption = rs(3).Value
    lblAmontDol.Caption = rs(4).Value
    lblTotalDol.Caption = rs(5).Value
    CmdExchangDol.Caption = rs(6).Value
    CmdClearDol.Caption = rs(7).Value
End Sub

Sub switchtokh()
   CmdExhangeKh.FontName = "Khmer OS Battambang"
   CmdClearKh.FontName = "Khmer OS Battambang"
   CmdExchangeDol.FontName = "Khmer OS Battambang"
   CmdClearDol.FontName = "Khmer OS Battambang"
   lblTitle.FontName = "Khmer OS Battambang"

   Set db = CurrentDb
   Set rs = db.OpenRecordset("Select * FROM tblKh")
   lblTitle.Caption = rs(0).Value
   Kh.Caption = rs(1).Value
   Dol.Caption = rs(2).Value
   lblRateKh.Caption = rs(3).Value
   lblAmountKh.Caption = rs(4).Value
   lblTotalKh.Caption = rs(5).Value
   CmdExhangeKh.Caption = rs(6).Value
   CmdClearKh.Caption = rs(7).Value

   lblRateDol.Caption = rs(3).Value
   lblAmounDol.Caption = rs(4).Value
   lblTotalDol.Caption = rs(5).Value
   CmdExchangDol.Caption = rs(6).Value
   CmdClearDol.Caption = rs(7).Value

   rs.Close
   Set rs = Nothing
   Set db = Nothing

End Sub

Private Sub txtRateKh_KeyPress(KeyAscii As Integer)
'Cannot type letters
    If KeyAscii >= 47 And KeyAscii <= 57 Or KeyAscii = 8 Then
       Exit Sub
   Else
       KeyAscii = 0
   End If
End Sub

Private Sub txtAmountKh_KeyPress(KeyAscii As Integer)
   If KeyAscii >= 47 And KeyAscii <= 57 Or KeyAscii = 8 Then
      Exit Sub
   Else
      KeyAscii = 0
   End If
End Sub

Private Sub txtAmountDol_KeyPress(KeyAscii As Integer)
   If KeyAscii >= 47 And KeyAscii <= 57 Or KeyAscii = 8 Then
       Exit Sub
   Else
       KeyAscii = 0
   End If
End Sub

Private Sub txtRateDol_KeyPress(KeyAscii As Integer)
    If KeyAscii >= 47 And KeyAscii <= 57 Or KeyAscii = 8 Then
       Exit Sub
    Else
       KeyAscii = 0
    End If
End Sub
   

Comments

pheanh comment

 pheanh

exchanger



2017-05-26



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.