vb.net - Exporting an access DB to Excel through VB - with the first 10 rows of DB -
i new using vb alongside excel , in need of help.
i have 2 access databases; 1 having 10 columns many rows (more 10), , other database has 8 columns , same amount of rows other.
what trying export first 10 rows of both databases excel sheet (in separate sheets or in 2 separate excel files, either way) emailed or printed.
i've been looking around trying idea on how , have tried few ways of doing none of them have worked.
additionally i'll fine if can exporting 1 database through vb first 10 rows of database.
could please me.
thanks, andy
assuming import data button in excel, want use adodb connect access databases, select data recordset, read recordset array , assign worksheet want import (optionally @ end of existing data - depends on whether you're importing "10 more rows" or refreshing top 10 rows (whatever means - guessing have query these in access, though).
approach this. oh, before begin, you'll need add reference (tools -> references) microsoft activex data objects 6.1.
public sub test() dim strsql_query string dim ocn adodb.connection dim ocmd adodb.command dim orecords adodb.recordset dim strcn string dim strdbpath string dim varvalues variant dim wkstarget excel.worksheet dim lngrows long dim lngcols long '' replace path db. use dialog box let user choose db, if '' moves around or isn't found. strdbpath = "c:\myfolder\myaccessfile.accdb" strcn = "provider=microsoft.ace.oledb.12.0;data source=" & strdbpath & ";" & _ "persist security info=false;" '' replace query need strsql_query = "select top 10 [mytable] <conditions> order [column] desc" set ocn = new adodb.connection ocn.connectionstring = strcn ocn.open set ocmd = new adodb.command ocmd.activeconnection = ocn ocmd.commandtext = strsql_query set orecords = ocmd.execute if orecords.bof , not orecords.eof varvalues = orecords.getrows set wkstarget = thisworkbook.worksheets(1) '' might need 0 , 1 instead of 1 , 2 - forget lngcols = ubound(varvalues, 1) lngrows = ubound(varvalues, 2) wkstarget.range("a1", wkstarget.range("a1").offset(lngrows, lngcols)) = varvalues end if '' clean up... set orecords = nothing set ocmd = nothing ocn.close set ocn = nothing '' more stuff... '' , finish saving workbook '' 1) check if directory exists , create dim strdir string strdir = "c:\some\path\here" if dir(strdir, vbdirectory) = "" mkdir(strdir) thisworkbook.saveas strdir & "yourworkbookname.xlsm", 52 '' 52 constant indicating macro enabled format end sub
Comments
Post a Comment