Estrapolazione dati da fonte e



  • Estrapolazione dati da fonte e
    di Giuseppe (utente non iscritto) data: 02/03/2011

    Buonasera a tutti.
    con una macro preparatami una decina di anni fa da un programmatore, estrapolo in excel dal programma di contabilità tutte le registrazioni
    che in un campo del gestionale contengono il carattere "/".
    le tabelle sono due: moco (contiene i dati dei movimenti non storicizzati) e stomov (contiene i movimenti storicizzati).
    con un macro poi modificata in vba prende i movimenti dalle due tabelle e me li inserisce uno sotto l'altro in un file di excel.
    il codice è il seguente:
    sub query()
    '
    ' query macro
    ' macro creata il 22/01/99
    '
    dim crit, zz, kk as string
    dim i as integer
    dim cicla as boolean
    'dim aggiorna as boolean
    dim primorange as string
    'dim secondorange as range

    'aggiorna = false

    sheets("db").select
    if worksheets("db").range("j2") = "" then
    ' aggiorna = true
    crit = inputbox("inserire il criterio di query", "query", default)
    worksheets("db").range("j2") = crit
    end if

    worksheets("db").range("a1:f1000").clear
    crit = worksheets("db").range("j2")

    zz = "select stomov.stodit, stomov.stondocfor, stomov.stodtdoc,
    stomov.stodesagg, stomov.stoimporto" & chr(13) & "" & chr(10) & "from
    stomov stomov" & chr(13) & "" & chr(10) & "where (stomov.stodesagg
    like " & chr(39) & chr(37) & crit & chr(37) & chr(39) & ")"
    with worksheets("db").querytables.add(connection:="odbc;dsn=gecom", _
    destination:=range("a1"))
    .sql = array(zz)
    .adjustcolumnwidth = false
    .fieldnames = true
    .refreshstyle = xloverwritecells
    .rownumbers = false
    .filladjacentformulas = false
    .refreshonfileopen = false
    .hasautoformat = true
    .backgroundquery = false
    .tablesonlyfromhtml = true
    .refresh backgroundquery:=false
    .savepassword = true
    .savedata = true

    end with

    ' msgbox worksheets("db").selection

    'else
    ' range("a2").select
    ' selection.querytable.refresh backgroundquery:=false

    'end if

    'if aggiorna then

    i = 2
    while worksheets("db").cells(i, 1) <> ""
    i = i + 1
    wend

    primorange = "a" & i
    ' worksheets("db").cells(5, 7).value = primorange
    'msgbox i & " " & primorange

    sheets("db").select

    zz = "select moco.mcdit1, moco.mcndocfor, moco.mcdtdoc, moco.mcdesagg,
    moco.mcimporto" & chr(13) & "" & chr(10) & "from moco moco" & chr(13)
    & "" & chr(10) & "where (moco.mcdesagg like " & chr(39) & chr(37) &
    crit & chr(37) & chr(39) & ")"
    with worksheets("db").querytables.add(connection:="odbc;dsn=gecom",
    destination:=range(primorange))
    .sql = array(zz)
    .adjustcolumnwidth = false
    .fieldnames = false
    .refreshstyle = xloverwritecells
    .rownumbers = false
    .filladjacentformulas = false
    .refreshonfileopen = false
    .hasautoformat = true
    .backgroundquery = false
    .tablesonlyfromhtml = true
    .refresh backgroundquery:=false
    .savepassword = true
    .savedata = true
    end with

    'else
    ' range("a300").select
    ' selection.querytable.refresh backgroundquery:=false
    ' end

    'end if

    cicla = true

    i = 2
    while cicla
    zz = worksheets("db").cells(i, 1)
    if zz = "" then
    cicla = false
    else
    zz = worksheets("db").cells(i, 4)
    kk = right(zz, 3)
    worksheets("db").cells(i, 6) = right(zz, 3)
    i = i + 1
    end if
    wend
    ' j = i

    ' i = 300
    ' cicla = true
    ' while cicla
    ' zz = worksheets("db").cells(i, 1)
    ' if zz = "" then
    ' cicla = false
    ' else
    ' zz = worksheets("db").cells(i, 4)
    ' kk = right(zz, 3)
    ' worksheets("db").cells(i, 6) = right(zz, 3)
    ' i = i + 1
    ' end if
    ' wend

    end sub

    adesso, siccome il gestionale ha inserito un'altra tabella (descrimoco) con tre campi collegati alla tabella moco ho fatto una
    query ottenendo al momento solo i dati non storicizzati con il seguente codice

    with activesheet.listobjects.add(sourcetype:=0, source:= _
    "odbc;dsn=gecom;remoteport=20222;server=nottheserver",
    destination:=range( _
    "$b$9")).querytable
    .commandtext = array( _
    "select moco.mcdtreg1, moco.mcndoc, moco.mccaus,
    descrimoco.descri_agg, moco.mcndocfor, moco.mcdtdoc, moco.mcdesagg,
    moco.mcimporto/100, moco.mcdit1, descrimoco.descri_ditta" & chr(13) &
    "" & chr(10) & "from descrimoco descrimoco, " _
    , _
    "moco moco" & chr(13) & "" & chr(10) & "where moco.mcdit1 =
    descrimoco.moc_descri_mcdit and moco.mcdtreg1 =
    descrimoco.moc_descri_mcdtreg and moco.mcprog =
    descrimoco.moc_descri_mcprog and ((descrimoco.descri_agg like '%@%'))"
    _
    )

    come posso modificare la stringa select ecc...... e il with ....... del codice originale in modo da ottenere solo i movimenti non storicizzati ma con il campo della nuova tabella correlata?
    spero di aver esposto bene il problema.
    giuseppe.