[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference abbott::visual_basic

Title:Microsoft Visual Basic
Moderator:TAMARA::DFEDOR::fedor
Created:Thu May 02 1991
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2565
Total number of notes:10453

2553.0. "Opening databases and using tables with odbc" by HIGHD::MELENDEZ () Thu May 08 1997 14:10

    Hello, I am trying to determine if it is possible
    to open a odbc database only once and then open the
    table I want to use only.  currently I have multiple
    data bound grids defined each one seems to connect to the
    database upon use of the form. What I have is about twenty
    Tables that I am accessing for different data, and I think
    this multiple connection is a waist of memory. 
    
    My questions relate to the use of data combo boxes.
    each one seems to require a datasource, such as data1
    What i would like to do is the following
    I have four combo boxes connected to a database.
    I would like to change the view of each box based on
    the settings of other boxes. Example: I have a grade code
    which defines how a metal is made.  There are different 
    requirements based on the size of the ingot, the diameter of
    the ingot, how it is melted, and a few other factors.
    In SQL I would write a command simular to this
    select * from gradetable 
    where gradecode = "gradevariable"
     and melttype ="melttype.melttype"
    and compacttype = "compact.compacttype"
    and ingotsize = "ingot.ingotsize;
    
    What I would like is to update the combo boxes so that only
    the data from the select would show in each box.
    such as the combo box for number of compact would be updated
    to show only the information that matches the previous fetch.
    
    Can you place default answers on Database combo boxes?
    Is there anyway to select which data item returned is viewed
    in the pulldown list first (such as an index)?  
    Can you update the rowsource datasource, listfield datafield on
    the fly? 
    Can you only open the database "connect once and then use tables
    as needed?
    Can I define the database in a module and then open tables from
    any form as needed?
    As I am new to Visual basic I beg your forgivness for being a goof!
    Thanks for any help
    Joe 
T.RTitleUserPersonal
Name
DateLines
2553.1EVTSG8::TOWERSFri May 09 1997 05:0525
    Joe, I've ranted on about this before so I'll try not to get too
    heated. It really depends what you're trying to do. Databound controls
    are fine for prototypes, for small applications with single user
    databases but are expensive and dangerous for large-scale systems with
    multi-user databases which require you to pay per connection.
    
    Apart from the issue you've discovered about the connection per control
    (the first VB3 app I worked on initially used databound controls to
    attach to an RDB database until we tested it with several users and
    brought the VAX to its knees), there's also the question of data
    integrity and locking. Basically, if you have a multi-user db you can't
    have databound controls and the data integrity you get from using
    locking.
    
    For larger projects there'a also the issue of maintainability. It's so
    much easier if all the code is in the code and not hidden in the
    properties of some of the controls. You know exactly where to look, but
    will the support guy who takes it over know where to look?
    
    The bottom line is use databound controls for fast prototyping,
    proof-of-concept type of stuff but write the serious stuff doing the
    hard work yourself using DAO or RDO or whatever.
    
    Cheers,
    Brian 
2553.2thanks for the reply HIGHD::MELENDEZFri May 09 1997 12:4527
    Hello Brian, Thanks for the prompt answer, I am sort of new to VB
    and have had no training so these things that are simple for others
    are somewhat hard for me.  I assume that from what your saying that
    I can program the code to do the open of the database and then open 
    and close the tables as needed, also understand about the locking 
    issue, most of my access will be reads from tables, with only a few 
    people actualy writting to the database. There are lots of notes here
    and I have not been able to come up with a search which gave me what I
    was looking for.  Can you give me some pointers to notes that will help
    or give me some direction. The ultimate aim is to open the connection
    once, open and close tables as needed, and lock only on the writes to 
    the tables. Now for the Stupid question of the day: when the VB
    application starts it opens form 1. Can I open the database in the form
    and use it from the other forms. such as the following.
    dim db as databse
    dim ws as workspace
    dim rs as recordset
    dim sconnect as string
    set ws = dbengine.createworkspace("ws1","username"," ")
    sconnect =" datbase connedt string"
    set db = ws.opendatabse(connect string) 
    from a different form then 
    set rs = form1.db.openrecordset(" sql select text)
    At the risk of being to far out there, I would ask for examples
    of how you have done this. 
    Thanks for your help and patience.
    Joe
2553.3EVTSG8::TOWERSMon May 12 1997 11:1534
    You've more or less got it, Joe. The only problem is one of scope of
    your variables.
    
    There are two approaches you can take. The old fashioned way is to have
    a module (eg DataAccess.bas) in which you declare your database
    variable using Global instead of Dim. A better approach is to create a
    DataAccess class and declare all these variables at the top level there
    (ie just after Option Explicit in the General section). Then
    concentrate all the DAO or RDO code in that class. In your Global.bas
    (or whatever you decide to call it) module declare:
    Global objDataAccess As New clsDataAccess
    and then in the rest of your code make calls to (Public) routines in
    your DataAccess class to do the work, eg 
    If objDataAccess.Logon Then
      Do some processing
      objDataAccess.LogOff
    Else
      Msgbox "Error message"
    End If
    
    This approach makes the code much easier to maintain and, if ever the
    day comes when you have to target another database/switch from DAO to
    RDO etc., it makes finding the code to change much easier.
    
    As to getting more info, the VB help is excellent. Just use the Search
    facility.
    
    Note that opening the database using DAO looks something like this:
    Set mdbMyDB = WS.OpenDatabase(strDBName, bFalseforshared, _
      bTrueforreadonly, strConnectionString)
    
    Cheers,
    Brian
    
2553.4thanks HIGHD::MELENDEZThu May 15 1997 12:516
    Hi Brian,
    Thanks for the good ideas. I will give them a try.
    sorry for not answering sooner but they removed the modem line in Las
    Vegas, Also the username extraction works well thanks for that also.
    Joe.