Macro to copy & paste but with conditions

jdshepp

New Member
Joined
Aug 4, 2010
Messages
1
Hi all

I need some help creating a macro for our church database. Basically I have reports for different missionaries. To generate the report I usually copy and paste information from a database of givers to a specific missionaries report.

What I would like the macro to do is to open the database and copy the information that relates specifically to a specific missionary. So the macro will go into the database and copy only information relating to missionary Joe Soap and then paste it into his report.

If anybody can help that would be awesome
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

wesimmo

Board Regular
Joined
Nov 23, 2005
Messages
149
I have recently taught myself to do this sort of thing, reading and writing to an Access database.

Its not something you'll be able to have someone knock up a couple of lines of code for and post on here so your job is done if i'm being honest.

I'd suggest you record a macro where you use the Excel functionality to grab data from your database and see if you understand how it all works, or at least the bits that you will need to edit.

For what its worth i have posted some code i ended up with, but it took me a day or so to get my head round it to be able to hack this together from recorded and googled code.

But essentially i got to this point by doing as i've advised above.

Code:
Sub Selector_Updater()
'Used to update the the Initiatives available for selection in the Initiative drop down


Dim adoCtn As ADODB.Connection
Dim adoRS As ADODB.Recordset

Dim wsSQL As String
Dim wsconn As String
Dim wsCount As Integer
Dim sOutput As String
Dim myCtl As Control
Dim SQL_Crit As String
Dim wsColumn As String
Dim x As Integer
Dim wsVarCou As Integer
Dim SQL_Cols As String
Dim wsInit_Concat As String
Dim wsList(1 To 50) As String




    


'Creates Required Columns

SQL_Cols = "ID, Title, Actioner"


'Creates Selection Criteria

SQL_Crit = ""

'Checks whether a control is a checkbox and whether it is true, if yes it picks up the value in the associated control
For Each myCtl In Me("Select_Criteria").Controls
        
        If TypeName(myCtl) = "CheckBox" And myCtl = True Then
            
            'picks up the field name
            wsColumn = Right(myCtl.Name, Len(myCtl.Name) - 6)
            If Me("Select_" & wsColumn) = "" Then
            
                x = MsgBox("YOU HAVE NOT ENTERED A CRITERIA FOR" & vbNewLine & vbNewLine & wsColumn, vbOKOnly)
                Exit Sub
            
            End If
            
                     
            SQL_Crit = SQL_Crit & "[" & wsColumn & "] = '" & Me("Select_" & wsColumn) & "' AND "
            
        End If
    
Next myCtl


If SQL_Crit = "" Then

    Exit Sub

End If

SQL_Crit = Left(SQL_Crit, Len(SQL_Crit) - 5)


'Creates SQL String
wsSQL = "SELECT " & SQL_Cols & " FROM Gen_Info WHERE " & SQL_Crit


'Sets up wsConn with database path and connection details
wsconn = "DSN=MS Access Database;" & _
        "DBQ=\\10.17.50.24\users$\wisimpson\Tracker\Initiative Tracker.accdb;" & _
        "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"


'Creates and opens ADODB connection
Set adoCtn = New ADODB.Connection

adoCtn.Open wsconn


'Creates and setups up a record set
Set adoRS = New ADODB.Recordset

adoRS.Open Source:=wsSQL, ActiveConnection:=adoCtn



    If Not (adoRS.BOF Or adoRS.EOF) Then 'If there are no records, this will be false
       adoRS.MoveFirst 'This probably isn't necessary, particularly with the default
                       'cursor type,but I always include it anyway
       
        wsCount = adoRS.Fields.Count
                        
        wsVarCou = 1
                        
        Do While Not adoRS.EOF Or adoRS.BOF 'Start looping through the records
            
            x = 0
            
            wsInit_Concat = ""
            
            Do While x < wsCount
                    
                wsInit_Concat = wsInit_Concat & adoRS.Fields.Item(x).Value & " - "
                
                x = x + 1
               
            Loop
                           
            wsInit_Concat = Left(wsInit_Concat, Len(wsInit_Concat) - 3)
            wsList(wsVarCou) = wsInit_Concat
                                                               
            wsVarCou = wsVarCou + 1
            
            adoRS.MoveNext  'When your code has been running for 1/2 an hour, you
                           'probably forgot this line.  I know I forget it all the time.
        
        Loop
    
        
    
    'Close stuff
    adoRS.Close
    adoCtn.Close
        
    'Get rid of the last comma
    ' sOutput = Left(sOutput, Len(sOutput) - 1)
    
    Else
        sOutput = "Empty Recordset"
    End If
    
    'Output
    Debug.Print sOutput
    
    'Clean up
    Set adoRS = Nothing
    Set adoCtn = Nothing


Sheets("Referencing").Range("Init_list").Clear
Sheets("Referencing").Range("Init_list").Select
wsVarCou = 1

Do While wsList(wsVarCou) <> ""

    ActiveCell.Value = wsList(wsVarCou)
    
    ActiveCell(2, 1).Select
        
    wsVarCou = wsVarCou + 1

Loop


Range("Init_list").Select
ActiveWorkbook.Names("Init_list").Delete
Range(ActiveCell, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Init_list", RefersTo:=Selection
Init_Pick.RowSource = "=Init_list"
DoEvents

End Sub
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,573
Messages
5,512,132
Members
408,881
Latest member
Pooxie

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top