Results 1 to 9 of 9

Thread: [RESOLVED] VBA and Access Database integration..help pls..??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Resolved [RESOLVED] VBA and Access Database integration..help pls..??

    Dear Experts,

    I am trying to make a VBA program from a "word document" that can collect information/data from Ms Access database...my question is:

    There are : Item Code , Descriptions, Qty, Unit Price and Total (all as TextBoxs except Item Code as a drop down menu)...what I am trying to do is, when I select Item Code from the drop down menu, it will collect all information (Description, Qty, Unit Price) and write into each TextBoxs and also it will calculate automatically for Total and write the total into the TextBox...

    Anyone could help me please..please..???

    Herewith, I attached a sample/dummy as a ZIP file for your reviews...and for my self learning...dont worry about other codes (such as how to write into the word documents, how to pop up, etc)....what I am trying to do is just how to make those codes run well....

    Thank you very much for your kind attention and helps.

    Best Regards,

    Jennifer
    Attached Files Attached Files

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA and Access Database integration..help pls..??

    Silly q's... But why are you coming from word to access, to put the data in a form?

    you probably need to look at ADO. Open a connection object to the database, a recordset (using an sql statement) to get the data and loop through it putting it on to the forms fields.

    Or if it is going into a mail merge, there might be a different option.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: VBA and Access Database integration..help pls..??

    Here's an example of a VBA function that can open an Access DB, and load a recordset with data. Also an example of using the routine. Have fun.
    VB Code:
    1. Function Try_DB()
    2.     ' Just an example of calling the Execute_SQL_ReturnRecordSet routine,
    3.     ' loading a recordset and getting some data from it.
    4.    
    5.     Dim rst As ADODB.Recordset
    6.     Dim x As Integer
    7.    
    8.     Set rst = Execute_SQL_ReturnRecordSet("Select * from Presidents", , _
    9.         "G:\Access\Access_Play.mdb")
    10.    
    11.     For x = 1 To rst.RecordCount
    12.         Debug.Print rst.Fields("Name").Value & " " & rst.Fields("DoB").Value
    13.        
    14.         rst.MoveNext
    15.     Next
    16.    
    17. End Function
    18.  
    19.  
    20. Function Execute_SQL_ReturnRecordSet(ByVal strQuery As String, _
    21.     Optional ByVal UpDateable As Boolean = False, _
    22.     Optional ByVal RemoteMDB As String = "") As ADODB.Recordset
    23.  
    24.     ' Requires reference to ActiveX Data Objects Library:
    25.     '   Tools - References - check Microsoft ActiveX Data Objects xx Library
    26.  
    27.     ' This accepts an SQL query string or table name & returns a recordset
    28.     ' call as:
    29.     '   Dim rst As ADODB.Recordset
    30.     '   Set rst = Execute_SQL_ReturnRecordSet ("Select * from Temp")
    31.     ' To allow modifications to the underlying table set UpDateable=True
    32.     ' To access a remote table in a different Access MDB file
    33.     ' specify its path\name in RemoteMDB
    34.  
    35.     Dim con As New ADODB.Connection
    36.     Dim rst As New ADODB.Recordset
    37.     Dim rstErr As New ADODB.Recordset
    38.  
    39.     con.Errors.Clear
    40.     On Error GoTo ErrBranch
    41.  
    42.     RemoteMDB = Trim(RemoteMDB)
    43.     If RemoteMDB = "" Or UCase(RemoteMDB) = "CURRENTDB" Then
    44.         'Set con = CurrentProject.Connection ' CurrentDB
    45.     Else
    46.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    47.             "Data Source=" & RemoteMDB
    48.         con.Open
    49.     End If
    50.  
    51.     If con.State = adStateOpen Then
    52.         Set rst.ActiveConnection = con
    53.         rst.CursorType = adOpenKeyset
    54.         If UpDateable Then
    55.             rst.LockType = adLockOptimistic
    56.         Else
    57.             rst.LockType = adLockReadOnly
    58.         End If
    59.     End If
    60.  
    61.     rst.Open strQuery
    62.     Set Execute_SQL_ReturnRecordSet = rst
    63.  
    64.     GoTo DoneSki
    65.  
    66. ErrBranch:
    67.     ' the DB wasn't opened. Create a dummy recordset
    68.     ' just so we can return something without erroring out.
    69.     ' When checked, the resulting rst.RecordCount=0
    70.     rstErr.Fields.Append "failed", adVarChar, 10, adFldMayBeNull
    71.     rstErr.Open
    72.     Set Execute_SQL_ReturnRecordSet = rstErr
    73.  
    74. DoneSki:
    75.     Set rst = Nothing
    76.     Set rstErr = Nothing
    77.     Set con = Nothing
    78.  
    79. End Function    ' Execute_SQL_ReturnRecordSet
    80. '#######################################################################
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: VBA and Access Database integration..help pls..??

    Hi,

    Thanks for your reply.

    Anyway...have you look into my vb form & my Ms Access please..???..because your example is really confusing me and did not solve my problem...and to ECNIV, yes it is...I just want to know how to get data from access and show it to each TextBoxs...and calculate the TOTAL....I really really NEW for VB Database...So, I really need helps...

    Thanks,

    Jennifer

  5. #5
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: VBA and Access Database integration..help pls..??

    I don't know how to get the form to display automatically, so I added a button to the document with the caption "Display The Form"

    Now, go to the VBA code window, the "IDE", to get there quickly press Alt-F11

    In the ThisDocument code section you'll see the code behind that button that simply opens your form:
    Private Sub CommandButton1_Click()
    UserForm1.Show
    End Sub

    I added the Execute_SQL_ReturnRecordSet function by:
    - creating Module1 (Insert – Module)
    - then pasting the function code into that

    See the code in UserForm1 for where the combobox is loaded
    and where it senses a selection to load the textboxes.

    Hopefully this will get started.
    Good luck, DaveBo
    Attached Files Attached Files
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: VBA and Access Database integration..help pls..??

    Hi Dave,

    Thank you for your kind reply and helps...Now is getting close to what I mean...that was looks like the document on the web...my questions are:

    How to make it on the Word Document instead of on the web view please? what code should I change for that?

    Secondly, how to calculate total on the vb form please..?? what kind of code to be used for calculating into the total TextBox in the vb form please..???...

    You dont have to worry to add all text into the Word template...once you let me know how to make "View" in the word document instead of as a website view, it will be great..!!

    Thanks for your kind helps..

    Jennifer

  7. #7
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: VBA and Access Database integration..help pls..??

    Quote Originally Posted by zach007
    How to make it on the Word Document instead of on the web view please? what code should I change for that?
    I'm not sure I understand. If you mean the "Normal View" vs. the "Web Layout View" in Word, just clicking on the desired icon near the bottom left of the Word window seems to work fine in either view.

    Here's a simple example of calculating the total and putting it into the txttotal text box.
    See the "' If txtqty & txtunit look like valid numbers" section in the middle of the routine below.

    VB Code:
    1. Private Sub cmb_item_Change()
    2.     Dim ItemCode As String
    3.     Dim x As Integer
    4.  
    5.     ItemCode = Trim$(cmb_item)
    6.  
    7.     If ItemCode <> "" Then
    8.         ' Restrict/filter the recordset by the selected Item_Code
    9.         Debug.Print "Item_Code=" & ItemCode
    10.         rst.Filter = "Item_Code = " & ItemCode
    11.    
    12.         ' The record should contain only 1 record right now
    13.         If rst.RecordCount = 1 Then
    14.             txtdes.Text = rst.Fields("Description").Value
    15.             txtqty.Text = rst.Fields("Qty").Value
    16.             txtunit.Text = rst.Fields("Unit_Price").Value
    17.            
    18.             ' If txtqty & txtunit look like valid numbers
    19.             ' then calculate a Total and put into txttotal textbox
    20.             If IsNumeric(txtqty) And IsNumeric(txtunit) Then
    21.                 'txttotal = txtqty * txtunit
    22.                 ' to fancy it up a bit you can do
    23.                 txttotal = Format$(txtqty * txtunit, "$#.00")
    24.             Else
    25.                 txttotal = "?"
    26.             End If
    27.  
    28.         ElseIf rst.RecordCount > 1 Then
    29.             MsgBox "There seem to be too many records for Item_Code " & ItemCode
    30.         Else
    31.             MsgBox "There seem to be no records for Item_Code " & ItemCode
    32.         End If
    33.     End If
    34.  
    35. End Sub

    Hope this helps. Merry Christmas & Happy Holidays, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    185

    Re: VBA and Access Database integration..help pls..??

    Hi Dave,

    Thank you for your kind response and helps...Hmmm, I think I should explain like this:

    When I select Item Code from its drop down menu, it will grab data from Ms Access database and write in each textbox for: "Description" and "Unit Price" only....then, we should put manually for "Qty"...but WHEN we "Tab" it will calculate the "Total"....

    Would you please help me for this..??

    Thanks many thanks for your helps..

    Jennifer

  9. #9
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: VBA and Access Database integration..help pls..??

    Here's one way to do it.
    This just detects a change in the qty textbox and recalculates the total each time.

    Add this to your UserForm1 VBA code
    VB Code:
    1. Private Sub txtqty_Change()
    2.     ' If txtqty & txtunit look like valid numbers
    3.     ' then calculate a Total and put into txttotal textbox
    4.     If IsNumeric(txtqty) And IsNumeric(txtunit) Then
    5.         'txttotal = txtqty * txtunit
    6.         ' to fancy it up a bit you can do
    7.         txttotal = Format$(txtqty * txtunit, "$#.00")
    8.     Else
    9.         txttotal = "?"
    10.     End If
    11. End Sub
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

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