?
Solved

Need help in coding a simple 4 button program connected to a DB using a Class Module.

Posted on 2003-03-30
14
Medium Priority
?
223 Views
Last Modified: 2010-05-01
I was given a simple 4 button application that I am to break down and duplicate.  Unfortunately the data that is to be pulled for the application is connected to a MS Access Database for its information.  

The 4 buttons include "Open Database", "Test Delete", "Test Insert" and "Test Update".

The database I have completed and its fields are PartNumber, Quantity In Stock, Cost and Sales Price.

So far, that sounds fairly simple.  

Here is the sticky part, all or most of the coding for the applications buttons is supposed to be done through a Class Module.

I have to create the code for the "OpenDB" method which accepts one argument, the file name of the Database to open.  

For the "Insert" button, I have to create 4 arguments which are all the fields of the database.

For the "Update" button, it should accept all the arguments that the "Insert" button has and update all the fields.

Finally, the "Delete" button deletes all records on the bases of the part number only.

The biggest problem I see with this is the fact that on the original application, I see underlined letters that are used as Ctrl shortcut keys and I'm not sure how those can be used since they are not menus, but cmdButtons.  I have worked with coding these types of programs in the past, but not using "Class Modules" and connecting these to one.  The other problem is that the program doesn't produce any visible output, therefore I can't see what is happening behind the scenes.

I apologize if this sounds extremely stupid, but being a nooby at this, I'm really at a loss with Class Modules and have no other help on this.

Any help would be greatly appreciated
Thanks
Sunshine
0
Comment
Question by:SunshineNShadow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:bistrica
ID: 8235276
Q: The biggest problem I see with this is the fact that on the original application, I see underlined letters that are used as Ctrl shortcut keys and I'm not sure how those can be used since they are not menus, but cmdButtons.  

A: Add command button. Set caption to "Capti&on".Character "o" will be underline when applications starts. Also, button will be activated with Ctrl + o.
0
 

Author Comment

by:SunshineNShadow
ID: 8242644
Really need more help on this please.
0
 
LVL 4

Expert Comment

by:bistrica
ID: 8243390
Start with simple things.
Create form add button and text box to enter db path or name.
The on click do the coding for opend db. Do you have an access help file? If yes then look for opendb.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:SunshineNShadow
ID: 8245089
The GUI is finished, I'm just having a bit of a problem with the code opening a database indirectly using a Class Module instead of opening the database directly with the button.

Like I mentioned, from what I understand, all the coding has to be done through the Class Module.
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 8262705
This is not as hard as you are making it. VB 6.0 has a wizard built into it that will do all of this for you. Add a new form to your project from the dialog select 'Data Form Wizard'. Then follow the prompts it will walk you right through all of this. It will even ask you if you want to use a class and it will build the class for you. It will connect to your database and stub out almost everything for you. You need to do some clean-up but it is trivial. When you are done you will have a reliable connection that will (from the start) perform Add, Insert, Update, Delete on your Access tables.

It's a handy tool.

Good Luck,

Rex
0
 

Author Comment

by:SunshineNShadow
ID: 8263895
I can't use the wizard, I wish I could.  This is a project and I have to do it the hard way.
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 8264118
You cannot use the wizard because it doesn't work or you are not allowed to? If you are not allowed to use it then it will be quite obvious if you did, correct? So here is what I would suggest. Go ahead and use the wizard (if it is available) and study the code it generates. This will give you a good foundation for building a solid class that exposes the methods you need.

Having said all that, you are in bad shape if you don't have the wizard. But the beauty of it all is that I do. I can go ahead and fire it up and connect it to the publisher database that is common to every single Microsoft App I have ever known and then I will post the class here or email it to you if you would prefer.

If you would like me to do this for you post back and tell me what you need. I'll get on it and get it to you.
0
 

Author Comment

by:SunshineNShadow
ID: 8264305
I'm not allowed to use it actually, but I do have the wizard.  I will try to do as you suggested and see if it comes up with a good class code I can use as a basis to start from.

Thanks for the help
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 8264339
No problem. The class code will show you how to go about it and give you some good ideas. It will also show you how to do some of the more advanced things. You should be able to abstract from it enough to get going though.

If you don't have it you may want to get Programming Windows in Microsoft Visual Basic 6.0 by Francesco Balena. It is an excellent book and has all the information you need to solve this problem.
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 8264493
Also, since Button_Click() events cannot take custom arguements you would have to come up with a custom implementation. A class is a good way to go as they provide a solid approach to solving most problems. You can design public methods of your class in a variety of ways.
For example:

You could implement public methods for Add, Update, Insert and Delete. Then implement all of them in the private members of the class.

Or you could have one public Method for Click and it takes an arguement of type action (add, update, insert, delete).
Then you could design four separate classes clsAdd, clsUpdate, clsInsert, clsDelete and have your public Click method call those classes. This is probably overkill but in larger programming problems it is a nice modular way to do things. It would allow you to easily add more action classes and then just call them in the Click method. This is great for COM and binary compatibility problems.

If you will not have to extend this class beyond the scope of Add, Update, Insert, Delete. I would design one class with those 4 methods being public members of the class.

Then in your GUI you would have an event for Delete_Click() that would look like this.

Private Sub cmdDeleteRec_Click()

    Dim c_DataServer as New clsDataServer

    If c_DataServer.Delete(ThisRecord) = 0 Then 'Record Deleted No Errors
        'Do Cleanup
        Set c_DataServer = Nothing
    Else
        MsgBox(c_DataServer.GetLastError)
    End IF

End Sub

''''''''''''''''''''''''''''''''''''''''''''''
Here is your public class definition for Delete
-----------------------------------------------

Public Function Delete(ByVal InRecord As String) As Long

    'Implicit call to connect to DataBase. Connect is defined as private and not publicly accesible.
    If Connect = True Then
        '*-- Your Delete Record Code Goes Here
        If RecordExists(InRecord) = True Then
            ' Call private method to delete the recordset.
            If DeleteRecord(InRecord) = 0 Then
                Delete = 0
                'Implicit call to Disconnect
                Disconnect
                Exit Function
            Else
                Delete = 1 'Or your long return value.
                Disconnect
                Exit Function
            End If
        Else
            Err.Raise, 799, "Record Does Not Exist."
            Delete = 799
            Disconnect
            Exit Function
        End If
    Else
        Err.Raise, 777, "Database refused connection."
        Delete = 777
        Exit Function
    End if

End Function    


End Sub
0
 

Author Comment

by:SunshineNShadow
ID: 8265841
Thank you, that helps tremendously.
0
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 2000 total points
ID: 8265868
Keep in mind that is a high level type of pseudo-code. It is 98% VB but I was not a compiler and had to wing it. So don't expect it to compile. Also you need to code the interfaces of the class such as GetLastError, Connect, Disconnect those all have to be implemented by you. Let me know if you need more help.
0
 

Expert Comment

by:CleanupPing
ID: 8531300
Hi SunshineNShadow,
This old question (QID 20568155) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9440547
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Accept rawinnlnx9's comment as Answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question