Hello All. Can someone please help me? I have been at this code for two weeks. I am a newbie at creating the following code in VB6. I want to open a database (SQL), read from the data tables, and write the data into an existing excel spreadsheet cell by cell. Can someone view the code below? It does have errors, because I do not know where to start to correct them and or to figure out what’s missing. Thank you in advance for your help.
'*************Imports - classes used from other namespaces ******
'****************************************************************
Imports System
Imports System.Xml
Imports System.Data
Imports System.Data.OleDb
Imports System.Diagnostics.FileVersionInfo
Imports System.IO
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Text
Imports Excel
'*************** Declarations - Main Form **********************
'***************************************************************
Public Main
Inherits System.Windows.Forms.Form
Dim myDataSet As DataSet
Dim myRow As DataRow
Dim myCol As DataColumn
Dim myConnection As OleDbConnection
Dim mySet1 As DataSet
Public Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'*************** Mouse Cursor ***********************
'Me.MousePointer = 0
'Me.Mouse
'*************** LSpec input on Commandline *******************
'**************************************************************
Dim arguments As [String]()= Environment.GetCommandLineArgs()
If arguments.Lenth > 1 Then
Fnumbers.Text = Trim$(UCase$(CommandLineArguments(1)))
Me.Show() 'Shows the form currently excuting code
Me.Cursor = Cursors.Default
TabControl1.SelectedIndex = 1 'Show the Log File Tab
Call Go
End
End If
End Sub
'Do I really need GotFocus and LostFocus?
Private Sub Go()
'***********ADODB Connection Object&Recordset Obj.& "Store SQL Commands"**********
Dim myConnection As New OleDbConnection
Dim myRecSet As New OleDbConnection
Dim SQLstr As String
'MyConnObj.Open_
Dim sString As String
sString = "ServerName=SQLNCI;" & _
"DatabaseName= VB6Code;" & _
"UserID= VBUser;" & _
"Password= codingisfun;"
'**************Read Number of Turns Per Section***********************************************
'*********************************************************************************************
SQLstr = "SELECT RegionID, count(*) As TerritoryCoun;"
SQLstr = SQLstr & "FROM Terriories;"
SQLstr = SQLstr & "GROUP BY RegionID;"
Dim myCommand As New OleDbCommand(SQLstr, myConnnection)
Dim myAdapter As New OleDbDataAdapter(myCommandILV)
Dim myCommandBuilder As New OleDbCommandBuilder(myAdapterILV)
myConnection.Open()
myDataSet = New DataSet
Try
myDataAdpCCA.Fill(myDataSet, "RegionID")
Catch ex As Exception
WriteStatusMessage(ex.ToString(), "Black", 12)
Exit Sub
End Try
End Sub
If myDataSet.Tables("RegionID").Rows.Count() = 0 Then
WriteStatusMessage (Trim$(UCase$(Fnumbers.Text)) & " Not Found in List")
MyExcel = Nothing
myWorkBooks = Nothing
myWorkBook = Nothing
sh = Nothing
myworksheet = Nothing
myWorkSheetCore = Nothing
myWorkSheetSummary = Nothing
Exit Sub
End If
'******** Write - Number of Turns Per Section **************************************************
'***********************************************************************************************
myworksheet.Name = "Region"
myworksheet = myWorkBook.Worksheets.Add()
For Each myworksheet In sh
If UCase(Mid(myworksheet.Name.ToString, 1, 5)) = "SHEET" Then
Try
myWorkSheet.Delete()
Catch ex As Exception
End Try
End If
Next
myWorkBook.Sheets("Region").Select()
System.Windows.Forms.Application.DoEvents()
For Each myDataRow In myDataSet.Tables("RegionID").Rows
If SummaryAndDetails.Checked = True Then 'double check
myworksheet.Cells(3, 1) = "Country"
myworksheet.Cells(3, 6) = "State"
myworksheet.Cells(4, 1) = "County"
myworksheet.Cells(4, 4) = "Address"
myworksheet.Cells(4, 6) = "City"
myworksheet.Cells(5, 2) = "Zip"
myworksheet.Cells(7, 2) = "FirstName1"
myworksheet.Cells(8, 2) = "FirstName2"
myworksheet.Cells(7, 4) = "LastName1"
myworkshhet.Cells(8, 4) = "LastName2"
myworksheet.Cells(4, 3) = myDataRow("RegionID")
End If
End Function