473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Object variable or With block variable not set

Hello

I am working on a project to export data from a query into an Excel file. I have a form with a button on it and the button when clicked runs a function that is supposed to run the query and store it into a recordset, create a new Excel file, dump the contents into the Excel file and do some cell formatting on it to make it look decent.

I have put it together piece by piece, and it works fine up until I try to add cell formatting. I get the error "Object variable or With block variable not set" in my code. I first got "Method 'Range' of object '_Global' failed" on the line when I selected a range of cells, but only after it ran successfully once. Looking around online it seemed that I needed to set my objects that I created to Nothing, so that Access would not have them held up anymore. I did that, or at least tried to set them to nothing, but it still doesn't work. With the code I have now, it gives the the Object variable or with block error, at line 59 of my code. I'm not sure if it is still the same problem of Access still not completely letting go from the first time the function ran or if it's something else. Does anybody have any suggestions?

thanks
Josh

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     'create and set connection to current database
  3.     Dim conn As ADODB.Connection
  4.     Set conn = CurrentProject.Connection
  5.  
  6.     'create and connect new recordset to the new connection
  7.     Dim myRecordSet As New ADODB.Recordset
  8.     myRecordSet.ActiveConnection = conn
  9.  
  10.     'pop up input box to retrieve Officer Badge
  11.     Dim strInput As String
  12.     strInput = InputBox("Enter Officer Badge", "Officer Badge")
  13.  
  14.     'assign the SQL query that pulls all records matching SOBadge to strSQL
  15.     Dim strSQL As String
  16.     strSQL = "SELECT tblDRIVEINPASS.SOBadge, tblDRIVEINPASS.* FROM tblDRIVEINPASS "
  17.     strSQL = strSQL + "WHERE ((tblDRIVEINPASS.SOBadge = '" & strInput & "'));"
  18.  
  19.     'populate the recordset with results from the query
  20.     myRecordSet.Open strSQL
  21.  
  22.     'create excel application to push query results to
  23.     Dim xlApp As Excel.Application
  24.     Dim xlWorkbook As Excel.Workbook
  25.     Dim xlsht As Excel.Worksheet
  26.     Set xlApp = CreateObject("Excel.Application")
  27.     Set xlWorkbook = xlApp.Workbooks.Add
  28.     xlApp.Visible = True
  29.  
  30.     Dim intRows As Integer
  31.     Dim qryResultsCols As Integer
  32.     intRows = 2
  33.  
  34.     xlApp.Sheets("Sheet1").Select
  35.  
  36.     'fill in column headings
  37.     For intCols = 1 To 13 Step 1
  38.         xlApp.Cells(1, intCols).Value = myRecordSet.Fields(intCols - 1).Name
  39.     Next intCols
  40.  
  41.     'fill in records from database
  42.     If (myRecordSet.EOF And myRecordSet.BOF) Then
  43.         'no records to pull - do nothing
  44.     Else
  45.         myRecordSet.MoveFirst
  46.         xlApp.Cells(1, 1).Value = "Officer Badge"
  47.  
  48.  
  49.         Do Until (myRecordSet.EOF)
  50.             For qryResultsCols = 0 To 12 Step 1
  51.                 xlApp.Cells(intRows, qryResultsCols + 1).Value = myRecordSet.Fields(qryResultsCols).Value
  52.             Next qryResultsCols
  53.  
  54.             myRecordSet.MoveNext
  55.             intRows = intRows + 1
  56.         Loop
  57.     End If
  58.  
  59.     xlApp.ActiveSheet.Range("A1:M1").Select
  60.     With Selection.Borders(xlEdgeLeft)
  61.         .LineStyle = xlContinuous
  62.         .Weight = xlThick
  63.         .ColorIndex = xlAutomatic
  64.     End With
  65.     With Selection.Borders(xlEdgeTop)
  66.         .LineStyle = xlContinuous
  67.         .Weight = xlThin
  68.         .ColorIndex = xlAutomatic
  69.     End With
  70.     With Selection.Borders(xlEdgeBottom)
  71.         .LineStyle = xlContinuous
  72.         .Weight = xlThick
  73.         .ColorIndex = xlAutomatic
  74.     End With
  75.     With Selection.Borders(xlEdgeRight)
  76.         .LineStyle = xlContinuous
  77.         .Weight = xlThick
  78.         .ColorIndex = xlAutomatic
  79.     End With
  80.     With Selection.Font
  81.         .Name = "GE Inspira Medium"
  82.         .Size = 12
  83.         .Bold = True
  84.     End With
  85.  
  86.  
  87. Set xlWorkbook = Nothing
  88. Set xlSheet = Nothing
  89. Set xlApp = Nothing
  90.  
  91. myRecordSet.Close
  92. Set myRecordSet = Nothing
  93.  
  94. End Sub
Mar 24 '08 #1
2 4650
Stewart Ross
2,545 Expert Mod 2GB
Hi. Problem is the use of the Selection method, which is available in Excel but not reliably when Access is using Excel as an automation server. You don't need it, and it is considerably faster to refer directly to ranges rather than select them first. Add an Excel range object variable object to your code and change the Select references in lines 60 and beyond as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim Cellrange as Excel.Range 
  2.     Set CellRange = xlApp.ActiveSheet.Range("A1:M1")
  3.     With CellRange.Borders(xlEdgeLeft)
  4.         .LineStyle = xlContinuous
  5.         .Weight = xlThick
  6.         .ColorIndex = xlAutomatic
  7.     End With
  8.     With CellRange.Borders(xlEdgeTop)
  9.         .LineStyle = xlContinuous
  10.         .Weight = xlThin
  11.         .ColorIndex = xlAutomatic
  12.     End With
  13.     With CellRange.Borders(xlEdgeBottom)
  14.         .LineStyle = xlContinuous
  15.         .Weight = xlThick
  16.         .ColorIndex = xlAutomatic
  17.     End With
  18.     With CellRange.Borders(xlEdgeRight)
  19.         .LineStyle = xlContinuous
  20.         .Weight = xlThick
  21.         .ColorIndex = xlAutomatic
  22.     End With
  23.     With CellRange.Font
  24.         .Name = "GE Inspira Medium"
  25.         .Size = 12
  26.         .Bold = True
  27.     End With
you may also find the discussion in the later part of the following post relevant for more detailed explanations: http://www.thescripts.com/forum/thread784045.html.

-Stewart
Mar 24 '08 #2
Awesome, that seems to have helped. Thanks for your suggestion
Mar 25 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
10
by: Michael McCracken | last post by:
Hi, I have a problem with unittest.TestCase that I could really use some help with. I have a class File in module File. The important thing about File for this discussion is that it's simple -...
8
by: Lauren Quantrell | last post by:
I get the following error: "Object variable or Width block variable not set error" trying to run this in my Access2000 .ADP database: CurrentDb.Properties.Append...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
6
by: Neo Geshel | last post by:
I am trying to deal with an image in code-behind. I consistently get the following error: Server Error in '/' Application. Object variable or With block variable not set. Description: An...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
1
by: abhijmenbumca07 | last post by:
Object variable or With block variable not set. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information...
4
by: BrianAdev | last post by:
Why It throw an error of <Object variable or With block variable not set> set objAccess = GetObject(strMdbPath) objAccess.DoCmd.SetWarning False objAccess.DoCmd.OpenReport strReportName, 0 'It...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.