Write code in VBA for customer data entry and generate bill

 Here's some example code in VBA that demonstrates how to create a basic data entry form for customer information and generate a bill based on that information:


'Form module

Private Sub cmdSave_Click()
    'Collect customer information from form
    Dim customerName As String
    customerName = Me.txtName.Value
    
    Dim customerAddress As String
    customerAddress = Me.txtAddress.Value
    
    Dim customerPhone As String
    customerPhone = Me.txtPhone.Value
    
    Dim customerEmail As String
    customerEmail = Me.txtEmail.Value
    
    'Collect product information
    Dim productName As String
    productName = Me.txtProductName.Value
    
    Dim productPrice As Double
    productPrice = Me.txtProductPrice.Value
    
    Dim quantity As Integer
    quantity = Me.txtQuantity.Value
    
    'Calculate total cost
    Dim totalCost As Double
    totalCost = productPrice * quantity
    
    'Generate bill
    Dim bill As String
    bill = "Invoice for customer " & customerName & vbNewLine & vbNewLine & _
    "Contact Information:" & vbNewLine & _
    "Address: " & customerAddress & vbNewLine & _
    "Phone: " & customerPhone & vbNewLine & _
    "Email: " & customerEmail & vbNewLine & vbNewLine & _
    "Order Information:" & vbNewLine & _
    "Product Name: " & productName & vbNewLine & _
    "Quantity: " & quantity & vbNewLine & _
    "Total Cost: " & totalCost & vbNewLine
    'display bill
    MsgBox bill
End Sub

This code creates a form with text boxes for customer name, address, phone number, email and product information, like product name, quantity, product price, and then calculates the total cost of purchase. The generated bill will be shown on the screen in the form of a message box.

Keep in mind that this is just an example and you can customize it to your needs and also include more fields as per requirement.
It would also be a good idea to include a way to save the bill to a file or database, depending on your application's needs.

Comments