Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2008, 09:33 AM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Method 'Range' of _Global' Failed accessing Excel

I'm trying to fix an sub routine in an VB module that basically reads in a MS database and writes it to an Excel Spread sheet. It works just fine except that the data isn't sorted correctly. I have no experience at all in excel or the vb code to access excel. I found a few lines of code by searching on how to sort in vb for excel and the code with the new sort logic works fine the first time you run it, but run it twice and you get the titled error. Here is the code they created and I marked the code I added. Any help would be deeply appreciated. Sorry ahead of time for length of code.

[VB]
Public Sub excel()

Dim indx As Integer
Dim rowIndex As Integer
Dim colIndex As Integer
Dim recordCount As Integer
Dim fieldCount As Integer
Dim MSG As String
Dim avRows As Variant
Dim excelVersion As Integer
Dim transType As String
Dim system As String
Dim sql As String
Dim TcrRecs()
Dim Oput As String

system = lstLOB.Text
transType = lstTransacType.Text

'CHECK FOR SEARCH TYPE
openconn

If lstLOB.SelCount = 0 And lstTransacType.SelCount = 0 Then
MsgBox "Search Requires A System/Transaction Or Both!", vbExclamation, "Error"
closeconn
Exit Sub
End If

If lstTransacType.SelCount > 0 Then
For a = 0 To lstTransacType.ListCount - 1
If lstTransacType.Selected(a) Then
If sql = "" Then
sql = " AND (TransacType = '" & lstTransacType.List(a) & "'"
Else
sql = sql + " or TransacType = '" & lstTransacType.List(a) & "'"
End If
End If
Next

Call rs("SELECT a.Name, b.TransacType, b.TestCaseNum, c.PolicyNum, b.TestScenarioDescription, c.Impact, c.ExpectedResults FROM Areas a, TestCases b, TestCaseExecution c WHERE a.AreaID = b.AreaID AND b.TestCaseID = c.TestCaseID " & sql & ") ORDER BY a.NAME, b.TransacType, b.TestCaseNum ASC")

End If

If lstLOB.SelCount > 0 And lstTransacType.SelCount = 0 Then
For a = 0 To lstLOB.ListCount - 1
If lstLOB.Selected(a) Then
If sql = "" Then
sql = " AND (name = '" & lstLOB.List(a) & "'"
Else
sql = sql + " or name = '" & lstLOB.List(a) & "'"
End If
End If
Next

Call rs("SELECT a.Name, b.TransacType, b.TestCaseNum, c.PolicyNum, b.TestScenarioDescription, c.Impact, c.ExpectedResults FROM Areas a, TestCases b, TestCaseExecution c WHERE a.AreaID = b.AreaID AND b.TestCaseID = c.TestCaseID " & sql & ") ORDER BY a.NAME, b.TransacType, b.TestCaseNum ASC")

End If

If adoRS.recordCount = 0 Then
MsgBox "There Were No Test Cases Found Matching Your Criteria", vbInformation, "Error"
closeconn
Exit Sub
End If

'THROWS THE RECORDSET INTO AN ARRAY
avRows = adoRS.GetRows()

recordCount = UBound(avRows, 2) + 1
fieldCount = UBound(avRows, 1) + 1

'CREATE REDERENCE VARIABLE FOR THE SPREADSHEET
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.add

Set objTemp = objExcel

excelVersion = Val(objExcel.Application.Version)
If (excelVersion >= 8) Then
Set objExcel = objExcel.ActiveSheet
End If

'PLACE THE NAMES OF THE FIELDS AS COLUMN HEADERS

With objExcel.Cells(1, 1)
.Value = "System"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 2)
.Value = "Trans Type"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 3)
.Value = "TC Nbr"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 4)
.Value = "In Prog"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 5)
.Value = "Req Nbr"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 6)
.Value = "Policy Nbr"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 7)
.Value = "Date"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 8)
.Value = "Tstr Intls"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 9)
.Value = "Test Scenario Description"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 10)
.Value = "Impact"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

With objExcel.Cells(1, 11)
.Value = "Expected Results"
.VerticalAlignment = xlVAlignTop
With .Font
.Name = "Arial"
.Bold = True
.Size = 11
.Italic = True
End With
End With

'MEMORY MANAGEMENT
adoRS.Close
Set adoRS = Nothing

'ADD THE DATA
With objExcel
For rowIndex = 2 To recordCount + 1

Oput = IIf(IsNull(avRows(1 - 1, rowIndex - 2)), "", avRows(1 - 1, rowIndex - 2))
'Oput = avRows(1 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 1).Value = Oput

' .Cells(rowIndex, 1).Value = avRows _
' (1 - 1, rowIndex - 2)
Oput = IIf(IsNull(avRows(2 - 1, rowIndex - 2)), "", avRows(2 - 1, rowIndex - 2))
'Oput = avRows(2 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 2).Value = Oput

' .Cells(rowIndex, 2).Value = avRows _
' (2 - 1, rowIndex - 2)
Oput = IIf(IsNull(avRows(3 - 1, rowIndex - 2)), "", avRows(3 - 1, rowIndex - 2))
'Oput = avRows(3 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 3).Value = Oput

' .Cells(rowIndex, 3).Value = avRows _
' (3 - 1, rowIndex - 2)

.Cells(rowIndex, 4).Value = " "
.Cells(rowIndex, 5).Value = " "

Oput = IIf(IsNull(avRows(4 - 1, rowIndex - 2)), "", avRows(4 - 1, rowIndex - 2))
'Oput = avRows(4 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 6).Value = Oput

' .Cells(rowIndex, 6).Value = avRows _
' (4 - 1, rowIndex - 2)

.Cells(rowIndex, 7).Value = " "
.Cells(rowIndex, 8).Value = " "

Oput = IIf(IsNull(avRows(5 - 1, rowIndex - 2)), "", avRows(5 - 1, rowIndex - 2))
'Oput = avRows(5 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 9).Value = Oput

' .Cells(rowIndex, 9).Value = avRows _
' (5 - 1, rowIndex - 2)

Oput = IIf(IsNull(avRows(6 - 1, rowIndex - 2)), "", avRows(6 - 1, rowIndex - 2))
'Oput = avRows(6 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 10).Value = Oput

' .Cells(rowIndex, 10).Value = avRows _
' (6 - 1, rowIndex - 2)
Oput = avRows(7 - 1, rowIndex - 2)
Oput = Replace(Oput, Chr(13), "")
Oput = Replace(Oput, Chr(9), "")
.Cells(rowIndex, 11).Value = Oput
' .Cells(rowIndex, 11).Value = avRows _
' (7 - 1, rowIndex - 2)

Next
End With

objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
objExcel.Cells(1, 1).CurrentRegion.VerticalAlignment = xlVAlignTop
objExcel.Cells(1, 1).CurrentRegion.WrapText = True

' This what I added********************************************* **********
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1") _
, Order2:=xlAscending, Key3:=Range("B1"), Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
'************************************************* ************************

closeconn

End Sub

[/VB]






Similar Threads
Thread Thread Starter Forum Replies Last Post
Method 'Open' of object '_Recordset' failed trom2004 Access VBA 7 December 14th, 2006 01:30 AM
Accessing a VBA module's method from VBScript hamffjs Access VBA 3 November 28th, 2006 09:27 AM
login Failed on my report using PUSH Method melvik Crystal Reports 1 November 9th, 2006 08:35 AM
Method Range - VBA - Excel teatimer Beginning VB 6 0 May 22nd, 2006 03:50 AM
METHOD 'RANGE' OF OBJECT '_GLOBAL' FAILED CBCHIAM Excel VBA 7 August 31st, 2005 04:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.