Solved

Compile error: user-type not defined

Posted on 2007-03-22
9
356 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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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 119

Expert Comment

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

modExportToExcel

0
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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 119

Accepted Solution

by:
Rey Obrero 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 119

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now