Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting data??

Status
Not open for further replies.

ctodd321

Programmer
Joined
Apr 6, 2000
Messages
2
Location
US
Hi, <br>I need to export data to an excel sheet or some kind of database(acess?) which is obtained via a program I worte in VB. The program reads the value from an instrument and then displays it in a dialog box. I need to then take it from the dialog box, add a time stamp to it and send the value to a database table or spreadsheet. <br>How do I go about this??!!??!<br><br>Thanks for your help.<br>Todd<br>
 
Todd -<br><br>Here's a function I wrote to copy from a datacontrol to a spreadsheet.&nbsp;&nbsp;You should be able to easily adapt it to only copying one row of data!<br><br>There's some debug code in there (the block of code surrounded by '#' marked lines).&nbsp;&nbsp;I use a compiler variable called 'dodebug' to switch in/out my debug statements.&nbsp;&nbsp;I set it to various values to enable different levels of debugging (just '1', in this case).<br><br>Chip H.<br><br><br><FONT FACE=monospace>Public Sub DoCopyToExcel(dc As Data)<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim xlApp As Object<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim xlSheet As Object<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim xlRange As Object<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim i As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim j As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim iAnswer As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Const EXCEL_OLE_KEY = &quot;Excel.Application&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim ColumnCount As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim RowCount As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim RangeName As String<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>&nbsp;&nbsp;&nbsp;&nbsp;Screen.MousePointer = vbHourglass<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlApp = GetObject(, EXCEL_OLE_KEY)<br>&nbsp;&nbsp;&nbsp;&nbsp;If Err.Number &lt;&gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Excel was not running<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Err.Clear<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set xlApp = CreateObject(EXCEL_OLE_KEY)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Err.Number &lt;&gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set xlApp = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Screen.MousePointer = vbDefault<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Unable to start a copy of Excel.&quot;, vbOKOnly, &quot;Error&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub '&lt;-------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;Err.Clear<br><br>&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo DoCopyToExcel_Err<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;' Hide excel while performing stuff below.<br>&nbsp;&nbsp;&nbsp;&nbsp;xlApp.Visible = False<br>&nbsp;&nbsp;&nbsp;&nbsp;xlApp.DisplayAlerts = False<br>&nbsp;&nbsp;&nbsp;&nbsp;xlApp.workbooks.Add<br>&nbsp;&nbsp;&nbsp;&nbsp;xlApp.workbooks(1).Activate<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlSheet = xlApp.ActiveSheet<br>&nbsp;&nbsp;&nbsp;&nbsp;dc.Recordset.MoveFirst<br>&nbsp;&nbsp;&nbsp;&nbsp;ColumnCount = dc.Recordset.Fields.Count<br>&nbsp;&nbsp;&nbsp;&nbsp;RowCount = dc.Recordset.RecordCount<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;For i = 1 To RowCount + 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For j = 1 To ColumnCount<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#If dodebug &gt; 1 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Before setting cell(&quot; & CStr(i) & &quot;,&quot; & CStr(j) & &quot;) to value: &quot; & dc.Recordset.Fields(j - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If i = 1 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'show column headings<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set xlRange = xlSheet.Cells(i, j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xlRange.Value = dc.Recordset.Fields(j - 1).Name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set xlRange = xlSheet.Cells(i, j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xlRange.Value = CStr(dc.Recordset.Fields(j - 1))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set xlRange = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next j<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If i &gt; 1 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.Recordset.MoveNext<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Give user a chance to bail out<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If i Mod 200 = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iAnswer = MsgBox(&quot;200 rows done.&nbsp;&nbsp;Continue?&quot;, vbYesNo, &quot;Confirm&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If iAnswer = vbNo Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit For '&lt;=======<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;Next i<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;RangeName = NumberToExcelColumn(1) & &quot;:&quot; & NumberToExcelColumn(ColumnCount)<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlRange = xlSheet.Columns(RangeName)<br>&nbsp;&nbsp;&nbsp;&nbsp;xlRange.AutoFit<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;xlApp.Visible = True<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlRange = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlSheet = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlApp = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Screen.MousePointer = vbDefault<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub '&lt;-------<br>&nbsp;&nbsp;&nbsp;<br>DoCopyToExcel_Err:<br>&nbsp;&nbsp;&nbsp;&nbsp;Set xlApp = Nothing<br>&nbsp;&nbsp;&nbsp;&nbsp;Screen.MousePointer = vbDefault<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub '&lt;-------<br>&nbsp;&nbsp;&nbsp;<br>End Sub<br><br>Private Function NumberToExcelColumn(lColNum As Long) As String<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim RVal As String<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If lColNum &lt;= 26 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RVal = Chr$(lColNum + Asc(&quot;A&quot;) - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RVal = Chr$((lColNum Mod 26) + Asc(&quot;A&quot;) - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RVal = Chr$((lColNum \ 26) + Asc(&quot;A&quot;) - 1) & RVal<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;NumberToExcelColumn = RVal<br>End Function<br></font><br><br>
 
Hey, <br>Thanks very much. I'm having trouble with a type mismatch error.<br>Getting the value to be recognized by your code?<br>I think its troube with the: dc as Data - How to convert something<br>from a textbox, i.e: 'variable.name' to the proper form (as data?)<br><br>Thanks, <br>Todd
 
Todd,<br><br>The 'As Data' means that he's passing the name of the Data Control to his function - so that he can read records from it (it's bound to some database or other) and put them into the Excel spreadsheet.<br><br>Have a look at these lines I've cut (from what looks like an excellent routine by the way):<br><br>Set xlApp = CreateObject(EXCEL_OLE_KEY)<br>xlApp.workbooks.Add<br>xlApp.workbooks(1).Activate<br>Set xlSheet = xlApp.ActiveSheet<br>Set xlRange = xlSheet.Cells(i, j)<br>xlRange.Value = dc.Recordset.Fields(j - 1).Name<br><br>These lines are the core of it and are very powerful, they:<br><br>open excel,<br>make a new workbook,<br>activate a worksheet,<br>set a range,<br>update those cells<br><br>the &quot;dc.Recordset.Fields(j - 1).Name&quot; bit is a field in a table - it's some data, the data that is being put in the range.<br><br>Have a play with a small VB app containing code like this - when you understand what goes on you should use all Chip's error checking code...<br><br>Mike <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top