Results 1 to 10 of 10
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Adding combined columns with an if statement otherwise add single column

    Hi ....



    Im trying to get my VBA to be a bit clever. What I would like to do is using my reference "sProjectRef" is have a calculation before the data gets added to a table. It currently adds Me.Project.Column(2) and combines it to Me.SubProjectCode.

    What I would like to do is IF Me.SubProjectCode column 4 contains "old code" then just use Me.SubprojectCode(2) as sProject and if Me.SubProjectCode is empty then combine "Me.Project.Column(2) & "-" & Me.SubProjectCode" as per the original entry.

    Code:
    Private Sub AddToSheet_Click()
    Dim sActivity As String
    Dim sProject As String
    Dim sHours As Double
    sDescript = Nz(Me.Description, " ")
    Me.Description = sDescript
    Dim Mysql As String
    Dim sLogUser As String
    Dim sDate As Date
    Dim sCostCode As String
    Dim sProjectRef As String
    Dim sHourType As String
    If Me.Activity = "" Then
        MsgBox "You must enter activity before continuing", vbInformation
        Me.Activity.SetFocus
        Exit Sub
    End If
    If Me.Project = "" Then
        MsgBox "You must enter a Project before continuing", vbInformation
        Me.Project.SetFocus
        Exit Sub
    End If
    If Me.LoggedInUser = "" Then
        MsgBox "You must enter activity before continuing", vbInformation
        Me.LoggedInUser.SetFocus
        Exit Sub
    End If
    sHourType = Me.Hour.Value
    sCostCode = Me.CostCode
    sActivity = Me.Activity.Column(0)
    sProject = Me.Project
    sHours = Nz(Me.Hour, 0)
    sDescript = Nz(Me.Description, " ")
    Me.Description = sDescript
    sLogUser = Me.LoggedInUser
    sDate = Me.DatePicker
    sProjectRef = Me.Project.Column(2) & "-" & Me.SubProjectCode
    If sHourType > 0 Then
    sHourType = "CD"
    Else
    sHourType = "CC"
    End If
        
    Mysql = "INSERT INTO TimesheetTableTemp (sCostCode, Activity, Project, Hours, HourType, Description, suser, ProjectRef, [task date]) Values "
    Mysql = Mysql & "('" & sCostCode & "', '" & sActivity & "', '" & sProject & "', " & sHours & ", '" & sHourType & "', '" & sDescript & "', '" & sLogUser & "', '" & sProjectRef & "', '" + Format(sDate) + "')"
    DoCmd.SetWarnings False
    DoCmd.RunSQL Mysql
    DoCmd.SetWarnings True
    Me!TimesheetTableSubform.Form.Requery
    ClearField
    End Sub
    Any ideas please?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What have you got so far? What part of this test are you stuck on? It sounds like just another If/Then/Else test like you've already used.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Am I close...
    Code:
    If Me.Project.Column(4) = "Old Code" Then
    sProjectRef = Me.Project.Column(2)
    Else
    sProjectRef = Me.Project.Column(3) & "-" & Me.SubProjectCode
    End If
    Merry Christmas

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Does it work? If not, what's wrong?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    It only adds the Me.SubprojectCode in regardless of whats selected, so no it doesnt work.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    So set a breakpoint and check the value coming from the combo. Perhaps you've got the wrong column.

    http://www.baldyweb.com/Debugging.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    I set a breakpoint but I dont think I am doing it correctly although I did read through the instructions. The process I did was:

    Set the breakpoint - its got a dot and is highlighted brown. It then stops at the point I spcecified then pressed F8 to step through but I dont get anything displayed.

    Just not getting it

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You can either hover the cursor over Me.Project.Column(4) or type

    ?Me.Project.Column(4)

    In the Immediate window.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Got this one to work in the end. The problem was I didnt have column 4 in my query so it was returning a null value. Added and now it works great - thanks!!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2011, 05:38 PM
  2. Replies: 3
    Last Post: 09-28-2011, 04:29 PM
  3. Replies: 1
    Last Post: 04-15-2010, 02:07 AM
  4. Adding a 0 to single digit imports
    By TheWolfster in forum Access
    Replies: 6
    Last Post: 02-16-2010, 05:19 PM
  5. Adding a single record
    By kfoyil in forum Forms
    Replies: 2
    Last Post: 11-22-2006, 09:12 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