Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Compile error: user-type not defined

Posted on 2007-03-22
9
365 Views
Last Modified: 2012-08-13
Hi Experts!

I can't get my code running in the module I created. I get this message from the compiler "User-defined type not defined". I thought that I forgot to define another variable, but everything seems fine. The error message highlights the command "Function ExportToExcel()" in yellow.

That's the code:
Function ExportToExcel()

    Dim Db As DAO.Database
    Dim rst As DAO.Recordset, RstDay As DAO.Recordset
    Dim appexcel As Object  'Excel.Application
    Dim wbexcel As Object   'Excel.Workbook
   
    Dim I As Integer, j As Integer
    Set Db = CurrentDb
   
    Set RstDay = Db.OpenRecordset("Select Distinct EventDay From TblCalendar")
'    Set RstDay = Db.OpenRecordset("SELECT TblCalendar.EventDay, '~' & [DirectorateID] & '~' & [Calendar_E] AS Expr1, TblCalendar.StatID FROM TblCalendar ORDER BY TblCalendar.EventDay DESC , TblCalendar.StatID;")
   
    Set appexcel = CreateObject("Excel.Application")
    appexcel.Visible = True
    Set wbexcel = appexcel.Workbooks.Open("C:\Documents and Settings\mconea\My Documents\Base de données\Exemples\EMS RO Calendar Excel.xls")
    'Set wbexcel = appexcel.Workbooks.Open("C:\EMS Calendar Excel.xls")

    appexcel.Sheets("Sheet1").Select
   
       
    Do Until RstDay.EOF
        'I = 4
        ' Search of the column to fill
        Select Case RstDay("EventDay")
            Case 36: j = 1
            Case 35: j = 2
            Case 34: j = 3
            Case 33: j = 4
            Case 32: j = 5
            Case 31: j = 6
            Case 30: j = 7
            Case 29: j = 1
            Case 28: j = 2
            Case 27: j = 3
            Case 26: j = 4
            Case 25: j = 5
            Case 24: j = 6
            Case 23: j = 7
            Case 22: j = 1
            Case 21: j = 2
            Case 20: j = 3
            Case 19: j = 4
            Case 18: j = 5
            Case 17: j = 6
            Case 16: j = 7
            Case 15: j = 1
            Case 14: j = 2
            Case 13: j = 3
            Case 12: j = 4
            Case 11: j = 5
            Case 10: j = 6
            Case 9: j = 7
            Case 8: j = 1
            Case 7: j = 2
            Case 6: j = 3
            Case 5: j = 4
            Case 4: j = 5
            Case 3: j = 6
            Case 2: j = 7
            Case 1: j = 1
            Case 0: j = 2
            Case -1: j = 3
            Case -2: j = 4
            Case -3: j = 5
            Case -4: j = 6
            Case -5: j = 7
            Case -6: j = 1
            Case -7: j = 2
            Case -8: j = 3
            Case -9: j = 4
            Case -10: j = 5
            Case -11: j = 6
            Case -12: j = 7
            Case -13: j = 1
            Case -14: j = 2
            Case -15: j = 3
            Case -16: j = 4
            Case -17: j = 5
            Case -18: j = 6
            Case -19: j = 7
           
        End Select
       
        Select Case RstDay("EventDay")
          Case 29, 28, 27, 26, 25, 24, 23
            I = 32
          Case 22, 21, 20, 19, 18, 17, 16
            I = 49
          Case 15, 14, 13, 12, 11, 10, 9
            I = 66
          Case 8, 7, 6, 5, 4, 3, 2
            I = 83
          Case 1, 0, -1, -2, -3, -4, -5
            I = 101
          Case -6, -7, -8, -9, -10, -11, -12
            I = 111
          Case -13, -14, -15, -16, -17, -18, -19
            I = 128
          Case Else
            I = 13
                   
        End Select
       
        LastLigne = False
       
        Do Until LastLigne
             If appexcel.Cells(I, j) <> "" Then ' if the row is not empty, the line advances
                I = I + 1
            Else
                LastLigne = True
            End If
        Loop
       
        Set rst = Db.OpenRecordset("Select * From TblCalendar Where EventDay='" & RstDay("EventDay") & "'")
       
        Do Until rst.EOF
            appexcel.Cells(I, j) = rst![BotStat]
            I = I + 1
            rst.MoveNext
        Loop
        RstDay.MoveNext
    Loop
   
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

End Function

Hope someone can find out

Thanks

stronghold888
0
Comment
Question by:stronghold888
  • 3
  • 3
  • 3
9 Comments
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 150 total points
ID: 18775327
Are you setting anything equal to some variable you have not defined (Dimmed)?
If Option Explicit is not present in the Declarations section of EVERY module, it's quite easy to set something equal to a variable that was never defined. By default, Option Explicit is not turned - unless you have 'explicity (no pun) set the "Require Variable Declaration' checkbox under Tools>>Options in the VBA editor.

So ... this is a good thing to check. IF ... you find it is not present in a given module, the put it in. Once it's in every module ... then do a Compile.  If there are any undefined variables ... they will now be exposed.

And of course ... check to see that there are no missing References  *MISSING ... will show if any are missing

mx
0
 
LVL 75
ID: 18775340
PS ...  this exact same error was just posted a few minutes ago by another person ... So ... I did a quick copy and paste :-)

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18775352
if you happened to name your module ExportToExcel
better rename it something like

modExportToExcel

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 4

Author Comment

by:stronghold888
ID: 18775447
Forgot to declare some variables in Tools/References. Now there's just one error message with this line "Set rst = Db.OpenRecordset("Select * From TblCalendar Where EventDay='" & RstDay("EventDay") & "'")"

capricorn1, change the export name to another name it does not work either

stronghold888
0
 
LVL 4

Author Comment

by:stronghold888
ID: 18775474
Just found out...I changed the "EventDay" type from Number to Text and now it works. Only thing, EventDay is a field conaining numbers, not text...so I think that I have to change the last line "Set rst = Db.OpenRecordset("Select * From TblCalendar Where EventDay='" & RstDay("EventDay") & "'")" to something different.
I just removed the "Distinct" from this line Set RstDay = Db.OpenRecordset("Select Distinct EventDay From TblCalendar")...but the last line does not work and I don't know how I can change it :(
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 350 total points
ID: 18775487
if you want eventday as number

Set rst = Db.OpenRecordset("Select * From TblCalendar Where EventDay=" & RstDay("EventDay"))
0
 
LVL 4

Author Comment

by:stronghold888
ID: 18775534
It works now.

Thanks a lot! :o)

stronghold888
0
 
LVL 75
ID: 18775616
Hey Cap ... can we go for 3 today on this error :-)

Thanks stronghold ...

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18775680
just  wait..... <lol>
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question