Solved

Compile error: user-type not defined

Posted on 2007-03-22
9
371 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
[X]
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
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

707 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