473,396 Members | 2,004 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,396 software developers and data experts.

Access 10 to Excel 10 Exportation only partially working, any ideas?

I am exporting some data from Access to Excel, I have used this code before and it worked perfectly. I have 2 tables which I am trying to export, contacts and company details. When clicking export on the contacts form, it works perfectly whereas clicking on the company export button does not work. It is using the exact same code and so I do not see what could be making one work and the other not... does anyone have any ideas? Or suggestions as to what I should check?

Thanks

Rebecca
Aug 23 '10 #1
7 1602
Stewart Ross
2,545 Expert Mod 2GB
Hi Rebecca. We'd need to see the code you are mentioning which worked before. Without it there is too little detail to even hazard a guess about what might or might not be wrong.

If you could post the code behind your command button etc. it would be appreciated.

Welcome to Bytes!

-Stewart
Aug 23 '10 #2
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function funexport(strfilter As String, intfunctionid As Integer)
  5. On Error GoTo Err_funErrorChecking
  6. Dim strObjectName As String
  7. Dim strFunctionName As String
  8. Dim strselect As String
  9. Dim rstdata As DAO.Recordset
  10. Dim db As Database
  11. Dim strPath As String
  12. Dim objXL As Object
  13. Dim objActiveWkb As Object
  14. Dim objActiveWrksheet As Object
  15. Dim intcolumn As Integer
  16. Dim rstexport As DAO.Recordset
  17.  
  18. Set db = CurrentDb
  19.  
  20. Set rstdata = db.OpenRecordset("select * from usystbl000_export where functionid = " & intfunctionid)
  21. strFunctionName = rstdata!Function
  22. strObjectName = rstdata!ObjectName
  23.  
  24. Set rstdata = db.OpenRecordset("select * from usystbl000_exportdetail where functionid = " & intfunctionid)
  25.  
  26. rstdata.MoveFirst
  27. Do Until rstdata.EOF
  28.     strselect = strselect & rstdata!FieldName & ", "
  29.     rstdata.MoveNext
  30. Loop
  31. strselect = Left(strselect, Len(strselect) - 2)
  32.  
  33. If strfilter = "" Then
  34.     Set rstexport = db.OpenRecordset("SELECT " & strselect & " FROM " & strObjectName)
  35. Else
  36.     Set rstexport = db.OpenRecordset("SELECT " & strselect & " FROM " & strObjectName & " WHERE " & strfilter)
  37. End If
  38.  
  39. If rstexport.RecordCount > 0 Then
  40.  
  41.     'browse folder option to create filename
  42.     strPath = BrowseFolder("Please select a folder for EXPORT")
  43.     If strPath <> "Cancelled" Then
  44.     'open excel
  45.  
  46.     Set objXL = CreateObject("Excel.Application")
  47.     objXL.Application.Workbooks.Add
  48.     objXL.Visible = False
  49.     Set objActiveWkb = objXL.Application.ActiveWorkbook
  50.     Set objActiveWrksheet = objActiveWkb.Worksheets("Sheet1")
  51.  
  52.     objXL.ScreenUpdating = False
  53.     objXL.DisplayAlerts = False
  54.  
  55.     'loop through header fields and create them
  56.         'Export details
  57.  
  58.     With objActiveWrksheet
  59.         .Range("A1").CopyFromRecordset rstexport
  60.         rstdata.MoveFirst
  61.         intcolumn = 1
  62.         Do Until rstdata.EOF
  63.             .Columns(intcolumn).NumberFormat = rstdata!Format
  64.             .Columns(intcolumn).ColumnWidth = rstdata!ColumnWidth
  65.             '.Columns(intcolumn).HorizontalAlignment = rstdata!Alignment
  66.             intcolumn = intcolumn + 1
  67.             rstdata.MoveNext
  68.         Loop
  69.  
  70.     End With
  71.  
  72.     objActiveWkb.SaveAs strPath & "\" & strFunctionName & "_" & Format(Now, "YYYYMMDDhhmmss") & ".xlsx."
  73.     objActiveWkb.Close SaveChanges:=True
  74.     objXL.Application.Quit
  75.     Set objActiveWrksheet = Nothing: Set objActiveWkb = Nothing: Set objXL = Nothing
  76.    End If
  77. End If
  78.  
  79. Exit_funErrorChecking:
  80.     Exit Function
  81.  
  82. Err_funErrorChecking:
  83.     Call funErrorChecking(Err.Description, Err.Number, Application.CurrentObjectName, "funexport")
  84.     Resume Exit_funErrorChecking
  85. Resume
  86. End Function
  87.  
Aug 23 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
Well, there's no obvious problem with the code concerned that I can see from checking the content.

Given that we don't have your application to hand and you do, you'll need to do some digging to find out exactly what happens when you click on the Company export button (as we need to know much more about what is happening than 'it doesn't work' to be able to give you suitable advice!).

You'll need to approach this from a debugging perspective and set a break point in the On Click event handler for that button, then use the F8 key to single-step through your code to find out what exactly is being done.

If you need some help on what debugging is about we have an Insights article on Debugging in VBA which may be of use, linked here.

-Stewart
Aug 23 '10 #4
I have done that and tried to work it out myself, basically what happens is an error appears 'too few parameters'. The code errors on this line: Set rstexport = db.OpenRecordset("SELECT " & strselect & " FROM " & strObjectName)
Presumeably this means an error with the specified recordset, I have looked at the recordset and cannot see what could be wrong with it. The recordset consists of the following fields if it helps: CompanyName, CompanyAddress, TownCity, Postcode, ContactNumber1, WebsiteAddress, VATNumber, CompanyContact, CompanyType
Aug 24 '10 #5
Don't worry now I've found the answer
Aug 24 '10 #6
Stewart Ross
2,545 Expert Mod 2GB
Hi Rebecca. For interest, what was wrong? The error message you mention normally arises if a field mentioned in the WHERE clause of the SQL statement does not correspond to a field that the SQL interpreter can access. This can happen if a field is renamed or removed, for example.

By the way, I notice that lines 20 and 24 in the code you posted duplicate each other; it is not a good idea to open a recordset and then re-open the same one for the same object variable. I'd guess that line 24 can be deleted, but you'd need to confirm this by testing. Line 20 cannot be, as the recordset is referred to immediately after.

-Stewart
Aug 24 '10 #7
Hi, If you read the table name opened in line 20 and 24 they are actually two different tables, export and export detail as they have different information within them. Yes the problem was that a field name in the table did not correspond to a field name in the query (it was quite a subtle difference so took me a while to notice).

Thanks for trying to help anyway!

Rebecca
Aug 25 '10 #8

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

Similar topics

0
by: ImraneA | last post by:
Hi there Have a database, where front-end interface allows user to select a ms access database. From there, standard tables are linked. Routine, that creates a spreadsheet, for each table a...
1
by: tkaleb | last post by:
I have to create output file in a text, MS Access, MS Excel and .dbf format from C# Win/ADO.NET application. Data are collected in DataSet and there is no problem to make text file. However, I have...
5
by: Gill Smith | last post by:
My Access Key is not working in the below code. <TD align="left" width="25%"><A href="javascript:OpenSearch()" accesskey="L"><IMG src="Images/MySeach.gif" align="left" border="0"></A></TD> Is...
3
by: James Wong | last post by:
Dear all, I have an old VB6 application which can create and access Excel object. The basic definition statements are as follows: Dim appExcel As Object Dim wkb1 As Excel.Workbook Dim wks1...
0
by: franjorge | last post by:
Hi, I have created two stored procedures via VB using this code: sql = "CREATE PROC " & nombre_proc & " AS SELECT *" & _ " From MBM_PUNTOS_SCE_SIN_COINCIDIR_SIEGE_FALTA_PM_NE_" & mes & _ "...
1
by: TechnoPup | last post by:
Greetings, I am very new to working with databases, and I am not sure how to go about structuring the query I need. What I have is an Access database with approx. 400,000 records in 5 fields. ...
4
by: Keith Wilby | last post by:
How controllable from Access VBA is Excel? I'm currently using automation to dump 2 columns of data into an Excel spreadsheet so that the end user can create a line graph based on it. Could the...
2
by: pixie | last post by:
Hi, I have some code that I have been using for more than a year. Excel VBA. Today it stopped working. I have no idea why. Here is the code: Range ("E1").Select If IsEmpty(ActiveCell) Then...
0
by: jayce | last post by:
Hi I am using an ADODB connection to link up an Access database to my core spreadsheet tool. I have created a loop to repeatedly: - download data from Access - analyse it in Excel, with data...
0
by: Nadirsha Muhammed | last post by:
How to access excel function wizard through C#
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:
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.