Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18

    Conditional data entry from the Access database to Excel by vba

    Hello


    I am in macro and vba is a beginner.
    I want to use a variable instead of the word “owner”, which is equal to one cell in sheet2.
    I did it and got it error.
    Can you help me?
    Thanks

    my code is:

    Option Explicit

    Sub getDataFromAccess()
    ‘ Click on Tools, References and select
    ‘ the Microsoft ActiveX Data Objects 2.0 Library

    Dim DBFullName As String
    Dim Connect As String, Source As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset
    Dim Col As Integer

    Cells.Clear

    ‘ Database path info

    ‘ Your path will be different
    DBFullName = “C:\Users\takyar\Documents\NorthWind.accdb”
    ‘ Open the connection
    Set Connection = New ADODB.Connection
    Connect = “Provider=Microsoft.ACE.OLEDB.12.0;”
    Connect = Connect & “Data Source=” & DBFullName & “;”
    Connection.Open ConnectionString:=Connect

    ‘ Create RecordSet
    Set Recordset = New ADODB.Recordset
    With Recordset
    ‘ Filter Data
    'Source = “SELECT * FROM Orders WHERE [Shipper ID] = 3 ”
    Source = “SELECT * FROM Customers WHERE [Job Title] = ‘Owner’ ”

    .Open Source:=Source, ActiveConnection:=Connection

    ‘ MsgBox “The Query:” & vbNewLine & vbNewLine & Source

    ‘ Write field names
    For Col = 0 To Recordset.Fields.Count – 1
    Range(“A1”).Offset(0, Col).Value = Recordset.Fields(Col).Name
    Next

    ‘ Write recordset
    Range(“A1”).Offset(1, 0).CopyFromRecordset Recordset
    End With
    ActiveSheet.Columns.AutoFit
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You got an error with this, or some effort with a variable that you're not showing us? Knowing the error and where it occurs would help. You use a variable would look like:

    Source = "SELECT * FROM Customers WHERE [Job Title] = '" & VariableName & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    When I define a variable: dim factor as integer
    And factor is equal to the value of A1 cell in sheet2
    The following line encounters an error:
    .Open Source: = Source, ActiveConnection: = Connection

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You still aren't saying what the error is, or shown how Source is populated. You can use this to see what it contains at runtime:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    This message is displayed when I run the macro
    "No value given for one or more required parameters"
    And I'm looking for the cause of it

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    And you still haven't shown your code. This worked

    DoCmd.OpenForm "EnterData", , , "ID = " & Me.ID
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    This is my code, of course, with a little change over the previous one

    Option Explicit

    Sub getDataFromAccess()
    ' Click on Tools, References and select
    ' the Microsoft ActiveX Data Objects 2.0 Library

    Dim DBFullName As String
    Dim Connect As String, Source As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset
    Dim Col As Integer
    Dim FACTOR As Integer

    FACTOR = Sheets("sheet2").Range("a1").Value

    Cells.Clear

    ' Database path info

    ' Your path will be different
    DBFullName = "E:\LoadExcel.accdb"
    ' Open the connection
    Set Connection = New ADODB.Connection
    Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
    Connect = Connect & "Data Source=" & DBFullName & ";"
    Connection.Open ConnectionString:=Connect

    ' Create RecordSet
    Set Recordset = New ADODB.Recordset
    With Recordset
    ' Filter Data
    Source = "SELECT * FROM Customers WHERE [Division] = 'owner'and [Gross Sales] =FACTOR "
    'Source = "SELECT * FROM Customers WHERE [Gross Sales] = 100 "

    .Open Source:=Source, ActiveConnection:=Connection

    ' MsgBox "The Query:" & vbNewLine & vbNewLine & Source

    ' Write field names
    For Col = 0 To Recordset.Fields.Count - 1
    Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
    Next

    ' Write recordset
    Range("A1").Offset(1, 0).CopyFromRecordset Recordset
    End With
    ActiveSheet.Columns.AutoFit
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing
    End Sub

  8. #8
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    I apologize for having taken your time
    How should I use this?
    DoCmd.OpenForm "EnterData",,,, "ID =" & Me.ID

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The variable has to be concatenated into the string:

    Source = "SELECT * FROM Customers WHERE [Division] = 'owner' and [Gross Sales] = " & FACTOR

    Sorry, the code in post 6 was a goof on my part.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    that was perfect
    thanks for your help
    Good luck

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    Sorry
    A little question

    In the above code, we filtered the data with two conditions and then entered Excel.
    Now can we clear the corresponding records from the database with the same two conditions?

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm more used to working from the Access side rather than the Excel side. Basically you execute delete SQL with the same criteria:

    "DELETE * FROM Customers WHERE [Division] = 'owner' and [Gross Sales] = " & FACTOR
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    Thankful
    I'll try this now

  15. #15
    m.vesali is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    18
    Yes you are right
    It was perfectly correct
    Thank you for your attention

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Excel refresh blocks Access data entry
    By simaonobrega in forum Import/Export Data
    Replies: 4
    Last Post: 06-30-2017, 11:01 AM
  2. Creating a database for end user data entry Access 2003
    By ethan.geerdes in forum Database Design
    Replies: 2
    Last Post: 02-19-2016, 08:23 AM
  3. Conditional Data Entry Requirement
    By JeffGeorge in forum Forms
    Replies: 13
    Last Post: 02-27-2015, 10:29 AM
  4. Replies: 0
    Last Post: 02-17-2013, 11:17 PM
  5. Conditional data entry access: intresting
    By shanky365 in forum Access
    Replies: 1
    Last Post: 07-20-2011, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums