Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Why do I get a compile error?

    Hi guys,



    Im getting a compile error for the following code:
    Code:
    Private Sub AddToSheet_Click()
    Dim sActivity As String
    Dim sDepartment As String
    Dim sProject As String
    Dim sHours As Integer
    Dim sUser As String
    Dim Msql As String
    
    sActivity = Me.Activity
    sDepartment = Me.Department
    sProject = Me.Project
    sHours = Me.Hours
    sUser = Me.User
    sDescription = Me.Description
    
    Mysql = "INSERT INTO TimesheetTable ( Activity, Department, Project, Hours, User ) SELECT '" & sActivity & "' AS Activity, '" & sDepartment & "' AS Deptartment, '" & sProject & "' AS Project, '" & sHours & "' AS Hours, '" & sUser & "' AS User,'" & sDescription & "' AS Description"
    DoCmd.SetWarnings False
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    End Sub
    It flags up method or data member not found - can anyone shed some light on this for me please?

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Which line does the compiler fail on? Your SQL has more values than fields. Try this for the SQL statement:

    Mysql = "INSERT INTO TimesheetTable (Activity, Department, Project, Hours, User, Description) Values ('" & sActivity & "', '" & sDepartment & "', '" & sProject & "', " & sHours & ", '" & sUser & "', '" & sDescription & "')"

    The apostrophe delimiter is needed for text data. I assume sHours is number. Use # as delimiter for date values.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Thanks but still no joy Below is what I am trying now

    Code:
    Private Sub AddToSheet_Click()
    Dim sActivity As String
    Dim sDepartment As String
    Dim sProject As String
    Dim sHours As Integer
    Dim sUser As String
    Dim Msql As String
    
    sActivity = Me.Activity
    sDepartment = Me.Department
    sProject = Me.Project
    sHours = Me.Hours
    sUser = Me.User
    sDescription = Me.Description
    
    Mysql = "INSERT INTO TimesheetTable ( Activity, Department, Project, Hours, User ) VALUES ('" & sActivity & "', '" & sDepartment & "', '" & sProject & "', " & sHours & ", '" & sUser & "', '" & sDescription & "')"
    DoCmd.SetWarnings False
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    End Sub
    It errors on Me.Hours - any thoughts? Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    You declared sHours as integer. Is it possible that data could have decimal? If so, need to declare as Double. Although I would expect a different error message for datatype conflict. Hour is a reserved word in VBA and Access. Should not use reserved words as names. This might be causing a conflict in the code. Either rename the field or try Me.[Hour] or Me![Hour].
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Oops yeah I did but I still get this error Method or Data member not found as screen grabbed here:

    http://postimage.org/image/1ptc75dqc/

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    The code is not changed, did you try my suggestion?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Appreciate your help on this and apologies, the previous error was because I was trying to add data to a field which is already populated - the name.

    I keep taking about 1 step forward and 10 steps back... Ive amended my code as I am binding the username part to the subform and the rest I am trying to submit using the click button - is this possible? If so it seems like its doing something but nothing gets added to the subform here is the current code:
    Code:
    Private Sub AddToForm_Click()
    Dim sActivity As String
    Dim sDepartment As String
    Dim sProject As String
    Dim sHours As Double
    Dim sDescription As String
    Dim Msql As String
    
    sActivity = Me.Activity
    sDepartment = Me.Department
    sProject = Me.Project
    sHours = Me.[Hour]
    sDescription = Me.[Description]
    
    Mysql = "INSERT INTO TimesheetTable (Activity, Department, Project, Hours, Description) Values ('" & sActivity & "', '" & sDepartment & "', '" & sProject & "', " & sHours & ", '" & sDescription & "')"
    DoCmd.SetWarnings False
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    End Sub
    I can see why people get frustrated with Access

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Put in a debug.print statement to see what values Access is dealing with.

    Debug.print "sActivity = " & Me.Activity & vbcrlf _
    & "sDepartment = " & Me.Department & vbcrlf _
    & "sProject = " & Me.Project & vbcrlf _
    & "sHours = " & Me.[Hour] & vbcrlf _
    & "sDescription = " & Me.[Description]




  9. #9
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Does this replace the current code or do I just add it at the bottom?

    Thanks

  10. #10
    SteveH2508 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2010
    Location
    Chelsea, London, England
    Posts
    117
    Put Option Explicit at the top of your module.

    It will then show you that you have declared MSql as String but you are using MySql variable.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Access isn't the issue here. It is VBA and SQL programming you are struggling with. All of the identified errors are spelling and syntax mistakes. Sorry, I did not spot the error in the variable spelling. Include Option Explicit in the header of every module and spelling errors like that could be revealed. Check out the link at bottom of my post to learn debugging techniques.

    Debug.Print would be temporary code just for the debugging effort. You would remove or comment it after issue resolved.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Put the Debug.print before the DoCmd.RunSQL line

    And as the others have said

    use Option Explicit at the top of the module. It will highlight differences in spelling and unDimmed variables

  13. #13
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Done and done but still it doesnt work... I dont get any error messages which is really throwing me. Just to confirm - is it ok to have a single bound entity and try and usa vba to add the rest to the subform? Code as ever - below
    Code:
    Option Compare Database
    Option Explicit
    Private Sub AddToSheet_Click()
    Dim sActivity As String
    Dim sDepartment As String
    Dim sProject As String
    Dim sHours As Double
    Dim sDescript As String
    Dim Mysql As String
    sActivity = Me.Activity
    sDepartment = Me.Department
    sProject = Me.Project
    sHours = Me.Hour
    sDescript = Me.[Description]
    Mysql = "INSERT INTO TimesheetTable (Activity, Department, Project, Hours, Description) Values ('" & sActivity & "', '" & sDepartment & "', '" & sProject & "', '" & sHours & "', '" & sDescript & "')"
    DoCmd.SetWarnings False
    Debug.Print
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    Me!TimesheetTableSubform.Form.Requery
    End Sub

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Please show us the output from the debug.print

    And use the whole debug.print

    Debug.print "sActivity = " & Me.Activity & vbcrlf _
    & "sDepartment = " & Me.Department & vbcrlf _
    & "sProject = " & Me.Project & vbcrlf _
    & "sHours = " & Me.[Hour] & vbcrlf _
    & "sDescription = " & Me.[Description]


    Also, your comment earlier
    I can see why people get frustrated with Access
    is little misleading.

    You have to learn Access, just like other software. It isn't a simple On/OFF, as you are discovering.

  15. #15
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Not having used debug.print what and where am I expecting an output? I have added the code as instructed before the DoCmd.RunSQL Mysql as here:
    Code:
    Option Compare Database
    Option Explicit
    Private Sub AddToSheet_Click()
    Dim sActivity As String
    Dim sDepartment As String
    Dim sProject As String
    Dim sHours As Double
    Dim sDescript As String
    Dim Mysql As String
    sActivity = Me.Activity
    sDepartment = Me.Department
    sProject = Me.Project
    sHours = Me.Hour
    sDescript = Me.[Description]
    Mysql = "INSERT INTO TimesheetTable (Activity, Department, Project, Hours, Description) Values ('" & sActivity & "', '" & sDepartment & "', '" & sProject & "', '" & sHours & "', '" & sDescript & "')"
    DoCmd.SetWarnings False
    Debug.Print "sActivity = " & Me.Activity & vbCrLf _
    & "sDepartment = " & Me.Department & vbCrLf _
    & "sProject = " & Me.Project & vbCrLf _
    & "sHours = " & Me.[Hour] & vbCrLf _
    & "sDescription = " & Me.[Description]
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    Me!TimesheetTableSubform.Form.Requery
    End Sub
    And I dont experience anything - not even a tickle under the chin. What am I doing wrong? Thanks

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Compile Error Syntax Error
    By Mohamed in forum Access
    Replies: 3
    Last Post: 10-06-2011, 10:12 AM
  2. Replies: 6
    Last Post: 09-28-2011, 09:20 PM
  3. Compile Error - Ambiguous Name ???
    By jacek.w.bak in forum Reports
    Replies: 1
    Last Post: 07-07-2011, 09:25 AM
  4. new compile error!
    By darklite in forum Access
    Replies: 6
    Last Post: 09-02-2010, 05:13 PM
  5. compile error
    By darklite in forum Access
    Replies: 6
    Last Post: 08-31-2010, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums