Results 1 to 34 of 34

Thread: Databases... oh my!

  1. #1

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125

    Unhappy Databases... oh my!

    I have finally came to face the fact that I HAVE to learn how to do database development. How should I start to learn databases? I tried to do Karl Moore's tutorial on it, but I don't want to use a flexgrid control or data control. How can I just connect to my database I made in Access, send the SQL query "Select <insert item number> From ItemNo"? My database name is Inventory.mdb, and my table name is Current.

    Thanks for any help
    -Joey
    <removed by admin>

  2. #2
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    Add a reference to microsoft Activex Data objects...


    Code:
    dim conn as adodb.connection
    dim rs as adodb.recordset
    
    conn.open "Provider=Microsoft.jet.oledb.4.0;Data source =" & app.path & "\Inventory.mdb"
    
    'it is needed that u have your DB in your project folder
     
    
    
    rs.cursorlocation = aduseserver
    rs.cursortype = adopenkeyset
    rs.locktype = adlockoptimistic
    
    rs.open "current",conn
    rs.addnew
    rs("field1") = "value1"
    'so on
    
    'update the table
    rs.update
    
    'close connections
    rs.close
    set rs = Nothing
    conn.close
    set conn = Nothing
    Hope this helps

  3. #3

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    veryJonny, Thanks for helping me, but that code you posted doesn't work. I added a reference to Microsoft ActiveX Data Objects (Multi-Demensional) 1.0 Library, and then put your code in my command button, but it didn't work. It said "User-defined type not defined."

    Is there a way to do this without a flexgrid, but with a data control? I think I will be able to use just a datacontrol, but I don't like the flexgrid.
    <removed by admin>

  4. #4
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    U added a wrong reference - add this :

    Microsoft ActiveX Data objects 2.5 Library



    to use the code u do not need Flexgrid/datacontrol.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You've set the reference to the wrong library.
    You should use Microsoft ActiveX Data Objects 2.x Library.

    Best regards

  6. #6

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Thanks. That must have been the problem. I'll try it.
    <removed by admin>

  7. #7

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    OK now I am having a different problem. I am using this code, just to test, but now it gives me an "Object variable or With block variable not set" error in the highlighted line:
    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3.  
    4. Private Sub Command1_Click()
    5. [color=yellow]conn.Open "Provider=Microsoft.jet.oledb.4.0;Data source=C:\Windows\Desktop\inventory.mdb"[/color]
    6. rs.CursorLocation = adUseServer
    7. rs.CursorType = adOpenKeyset
    8. rs.LockType = adLockOptimistic
    9.  
    10. rs.Open "current", conn
    11. MsgBox rs.RecordCount
    12. rs.Close
    13. conn.Close
    14. Set rs = Nothing
    15. Set conn = Nothing
    16. End Sub
    <removed by admin>

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You must create the objects before you can use them.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Set conn = New ADODB.Connection
    3.     Set rs = New ADODB.Recordset
    4.     'the rest of the code
    5. End Sub
    Best regards

  9. #9

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Thanks Joacim. That worked. I think I'll start to figure it out from here.
    <removed by admin>

  10. #10
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Sorry MidgetsBro

    My mistake.

    U shd change this line right at the top.
    Code:
    dim conn as newadodb.connection
    dim rs as new adodb.recordset

  11. #11

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Arg. Sorry to bother you guys again, you have been a great help. This is exactly why I have been avoiding databases. Now I get an Invalid SQL statement in the 'rs.Open "Current", conn' line. It says it expects DELETE, INSERT, PROCEDURE, SELECT, or UPDATE.

    Thanks for all the help you have given me so far. I hope I get this over with soon.
    <removed by admin>

  12. #12
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Shdnt happen in normal circum. , Are u trying to do something else?
    Last edited by veryjonny; Jan 3rd, 2002 at 01:28 AM.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You should change the word "current" to a valid SQL statement.
    VB Code:
    1. rs.Open "SELECT * FROM [i]TableName[/i]", conn
    Best regards

  14. #14

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    OK forget databases... I just fix one error, and I get another. I'm just not going to deal with databases anymore. I'll just write code to search a deliminated text file or somthing.

    Thanks for you help though, it got me somewhere at least. If I ever try to attack them again, then I'll have a headstart.
    <removed by admin>

  15. #15
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    Don't give up yet!
    Remember every error that crops up, and you solve by yourself, or others help you with, means you have climbed further up the learning curve.
    It is unfortunate the posts so far have used the inherently more complex ADO code.
    Although now going out of date, DAO code is much simpler for the beginner.
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

  16. #16

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Can you help then? I just want to connect to my database, use an SQL statement to select the item number I type it, then it returns the price for that item number. I just want two textboxes, one you type into, and then click the ok button, and it opens the database, retrieves the price for that item #, puts it in the second textbox, then closes the database.

    Can you show me how to do this with DAO? Is it really that much easier? I don't care if it is going out of date, I just want to know that I did SOMETHING with databases.

    Oh and about 3 days ago, my signature said trying is the first step towards failure... it still holds true for me, so I think I'll put it back in there.
    <removed by admin>

  17. #17
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    First add reference to Microsoft DAO v 3.6 to you project
    VB Code:
    1. Dim db As DAO.Database
    2. Dim rs As DAO.Recordset
    3. Dim strSQL As String
    4.  
    5. Set db = OpenDatabase(app.Path & "Inventory.MDB") 'change path here if not in project path
    6.  
    7. 'now construct SQL
    8.  
    9. strSQL = "SELECT * FROM Current WHERE YourField = ItemNo" 'substitute real field name and value of itemNo
    10.  
    11. 'open recordset
    12. Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    13.  
    14. If rs.RecordCount > 0 Then
    15.     Text1.Text = rs!YourField & "" 'substitute real field names
    16.     Text2.Text = rs!YourField2 & ""
    17. Else
    18.     MsgBox "No Match"
    19. End If
    20. rs.Close
    21. db.Close
    Last edited by gab2001uk; Jan 3rd, 2002 at 04:37 AM.
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

  18. #18
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking Cheese on a raft please...

    Try this, it uses the reference to ado (see above):

    Oh and it has 2 text boxes called txtPrice and txtItemNumber. Also there is a command button called cmdFetch.

    VB Code:
    1. Private Sub cmdFetch_Click()
    2. Dim adoConnection   As ADODB.Connection
    3. Dim adoRecordset    As ADODB.Recordset
    4. Dim strPath         As String
    5. Dim strSQL          As String
    6.     If Trim(txtItemNumber.Text) = "" Then
    7.         Beep
    8.         MsgBox "Please enter an item!"
    9.         Exit Sub
    10.     End
    11.     If Not IsNumeric(txtItemNumber.Text) Then
    12.         Beep
    13.         MsgBox "Item number must be numeric!"
    14.         Exit Sub
    15.     End If
    16.     strPath = "Path location of you database ie: C:\WinNT\Woof\"
    17.     Set adoConnection = New ADODB.Connection
    18.     adoConnection.CursorLocation = adUseServer
    19.     adoConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source =" & strPath & "\Inventory.mdb"
    20.     strSQL = "SELECT Price "
    21.     strSQL = strSQL & "FROM TableName "
    22.     strSQL = strSQL & "WHERE Item_Number = " & txtItemNumber.Text & " "
    23.     Set adoRecordset = adoConnection.Execute(strSQL)
    24.     With adoRecordset
    25.         If Not .EOF And Not .BOF Then
    26.             txtPrice.Text = Format(.Fields("Price"), "Currency")
    27.         Else
    28.             MsgBox "Item not found!"
    29.         End If
    30.     End With
    31.     adoRecordset.Close
    32.     Set adoRecordset = Nothing
    33.     adoConnection.Close
    34.     Set adoConnection = Nothing
    35. End Sub

    Does this work?

  19. #19
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    ADO or DAO does not really make much difference. Although using DAO with Access is fairly stronger than using ADO, there really is no harm in trying out ADO first.

    MidgetsBro, you might be very anxious and be upset on getting errors every now and then, but don't worry, you have experienced members (and I am not referring to myself ) to help you out.

    The errors you have encountered so far were very basic ones. First, you already know that before you can use objects from a class, you have to create an instance of the class, that's what you forgot to do with the Connection and the Recordset objects earlier. Next, with the rs.Open statement, you have to pass a valid SQL query, and "current" was not the valid SQL query. Imagine you are using SQL editor. Would you type "current" there to get your records? You would type "Select * from Current" and that's what you should use while opening a recordset.

    And also a working knowledge of the SQL language is required for manipulating the databases.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  20. #20
    Addicted Member
    Join Date
    Jun 2001
    Location
    UK
    Posts
    158
    Here's how I do it. And guys if this way is not efficient or if it is not a better way of working with DB Please let me know. B'cos no one taugth me how to work with db's . I learned these my self.

    I dont use References and all that. ( I dont know there names)

    Add a DataEnvironment to the Project
    right Click the Dataenvi.. and add a connection
    Change the propeties of the database. (Connect to your db)

    Ok that's it Now start coding
    Dim SQL
    Dim Rs as adodb.Recordset

    DataEnvironment1.Connection1.Open
    SQL = "SELECT * ...."
    Set Rs = DataEnvironment1.Connection1.Execute(SQL)
    SQL = "INSERT "
    DataEnvironment1.Connection1.Execute (SQL)
    DataEnvironment1.Connection1.Close

    If this doesn't work
    tell me I must have missed something
    [vbcode] On Error GoTo VBForums[/vbcode]
    www27.brinkster.com/muditha

  21. #21
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    If you want to open an entire table without building an SQL statement to do it, just specify that you're opening a table:
    VB Code:
    1. rs.Open "TableName", , , , adCmdTable
    As a general rule, I'd avoid using DataEnvironments and DataControls. They suck.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  22. #22
    Addicted Member goudabuddha's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere cheesy
    Posts
    203
    I hardly ever use databases, i just write stuff i need into a text files and i can specify the read/write functions as needed

  23. #23

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125

    Re: Well ...

    Originally posted by honeybee

    MidgetsBro, you might be very anxious and be upset on getting errors every now and then, but don't worry, you have experienced members (and I am not referring to myself ) to help you out.
    I don't get anxious when I get a few errors now and then, but with this, I got one error after the other. I asked a question, Joacim or vj answered, then a new error popped up when I fixed the old one. I didn't get the code to work at all. I will try that DAO code.

    Originally posted by goudabuddha

    I hardly ever use databases, i just write stuff i need into a text files and i can specify the read/write functions as needed
    That is EXACTLY what I do. I rather take the time to write functions to read and parse text files. It's much easier to me, but I am going to have to learn Databases sooner or later.

    PS... What the hell is the difference between ADO and DAO? A transposing of two letters?
    Last edited by MidgetsBro; Jan 3rd, 2002 at 03:28 PM.
    <removed by admin>

  24. #24

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    gab2001uk... your code worked for me! That is what I need. Just simple code that works, that I can build on. Thank you for helping me. I am going to try workawidget's code and see if I can get that to work, too.

    Thank you all for helping me. I feel like I have started on a good learning adventure through databases...
    <removed by admin>

  25. #25
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is an ADO example of what you wanted. I like examples I think they are easier to learn from.

  26. #26

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Thanks Edneeis, but I don't have the 2.5 library. Does that come with SP5? I only have SP4, and a 56k connection, so I can't download SP5. I'll look at the code though and study it. I got that DAO code that gab2001uk posted, so I think that should be enough for right now.
    <removed by admin>

  27. #27
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That's fine what version do you have. You can probably just change the reference to an eariler version, although I can't vouche for any of them before 2.5. Also you can download the latest MDAC without downloading the whole Service Pack. It would be my recommendation to just deal with the big download and get the service packs though, might save you a headache later if you run into one of the bugs they fixed.

    I did test the app with 2.0 and it worked.

  28. #28
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    Well at least between us we managed to keep you going, and not give up.
    Happy learning.
    Oh...The difference between DAO and ADO?
    DAO is the native code for Access databases, but because Micro$oft thought they might miss out on global domination, they invented ADO, which can connect to a larger number of (Rival) databases, and are actively trying to push it as the way to go.
    There are many things you can't do with ADO, and on one of their ADO sites, they tell you to use DAO code!

    DAO is also at least 2-5 times faster than the ADO equivalent.
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

  29. #29

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    I have two different versions of ADO. I have 2.0 which is msado20.tlb and I have 2.1 which is msado15.dll. I tried with both of them, and I got the same error with your code.
    <removed by admin>

  30. #30
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Who's code?

    Just reading this would deter me from using databases i dont know how you made sense of all this.

    Perhaps your problem is in your recordset line with the SQL statement. I.e your using adcmdTable instead of adcmdtext or something.

    One thing DAO is good but MS aint gonna be supporting it in the future as far as i know. Frankly if you use ADODB correctly then it would be hard to detect the difference in speed.
    I have several huge db's at work some in SQL some in access and i cant notice much difference between dao and ado i even tested a 1.5gb access db 1 prog used dao and 1 used ado and there was little to no difference.

    I couldnt put up with using text files that must take so long and be so annoying. Plus they be huge and lots of coding.

    later
    b

  31. #31

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Edneeis' code for ADO didn't work for me. It's just because I don't have the updated reference files that he used. I'm fine with DAO for right now. I don't need it for any big projects. I just wanted to know what it did finally. I know I might need it in the future, but for now, I know enough. I can probably use a database in my chat/filesharing program instead of loading my textfile into a listbox, and searching it for the username and password. I'll be fine for now. Thanks everyone who helped me.
    <removed by admin>

  32. #32
    Lively Member cargobay69's Avatar
    Join Date
    Nov 2001
    Location
    Kessel Prison Camp
    Posts
    97

    wrox again

    To learn all you need to know about VB and databases, I highly recommend Beginning Visual Basic 6 Database Programming by John Connell.
    Darrin@CB69
    -----------------------------------------------
    Arrogance kills brain cells
    -----------------------------------------------
    Private Sub Sandwich (big As Byte)
    On Error GoTo Pub

  33. #33
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What error did you get? Although it sounds like we are beating a dead horse here.

  34. #34

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    I get the same user-defined type error that I get if I don't add a reference to the dll with the DB functions in it. I think you are beating a dead horse... (that's a good analogy ) I've got the code to do what I need for now, cause it's just simply searching for an item number typed into the textbox, and finding it, and returning the price. Kind of like a cash register.
    <removed by admin>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width