Results 1 to 13 of 13
  1. #1
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14

    How to get pass the error I get when a field is blank

    Hey guys, I don't know how to bypass an error in my code. The code looks like this:



    Code:
    Private Sub Command29_Click()
    
    Dim rstTakeOut As DAO.Recordset
    
    Set rstTakeOut = CurrentDb.OpenRecordset("TakeOut", dbOpenDynaset)
    
    With rstTakeOut
        .AddNew
        .Fields("Date").Value = Text14.Value
        .Fields("WorkName").Value = Combo6.Column(1)
        .Fields("ProductName").Value = Combo9.Column(1)
        .Fields("ProductModel").Value = Combo12.Column(0)
        .Fields("Quantity").Value = Text59.Value
        .Fields("Price").Value = Text24.Value
        .Fields("Unit").Value = Combo12.Column(2)
        .Fields("ProductID").Value = Combo12.Column(3)
        .Fields("JobNumber").Value = Combo57.Column(1)
        .Update
    End With
    
    Me.Text59.Value = ""
    Me.Combo57.Value = ""
    Me.Combo6.Value = ""
    Me.Combo9.Value = ""
    Me.Combo12.Value = ""
    I'm filling in a table using data from the form. The Price from Text24 is automatically filled by a column in combo12. However the problem right now is that my price column is blank in my combo12 (because there is no value in the table that the combo box gets its info from). When I try this code, I get an error because of that. Is there anyway to just have it fill it with nothing when the price value in the original table is blank?

    Also a similar question. I have a calculation to subtract two DSum() functions in my VBA code. When one of the DSum() functions return nothing, instead of taking it as a 0, it just straight up doesn't give me an answer. Can I with it be 0 when there is no data found using the criteria for the DSum?
    Last edited by beanhead0321; 08-14-2011 at 09:30 PM. Reason: Additional Questions

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Look into the Nz() function.

  3. #3
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14
    So the Nz() function is like this:
    Nz(Variant,[value if null])

    For me, would it look something like

    Code:
    .Fields("Price") = Nz(Me.Text24.value,"0")
    
    Nz(Application.DSum(something, something, something),"0")

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The recordset should not have problem taking Null from the textbox control so don't know why this is erroring.

    Yes, use Nz function. Example: Nz(DSum(...),0)

    Numbers for numeric fields would not be in quotes.
    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
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    For these two examples you would probably want to supply a numeric 0 rather than a string "0".

  6. #6
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14
    so a numeric 0 would just be 0 instead of "0"?

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I don't think Access likes to put a Null in a numeric field.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by beanhead0321 View Post
    so a numeric 0 would just be 0 instead of "0"?
    Bingo! By jove I think you've got it.

  9. #9
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14
    Sweet! Thanks a bunch. I'll run it and see how it goes.

  10. #10
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14
    I'm getting a data type conversion error when I run this code.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    So at least one of the fields *is* a string. What are the DataTypes of the fields?

  12. #12
    beanhead0321 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    14
    It's okay. I got everything to work. The Nz function worked beautifully for the DSum functions. I used an IIf for the other one. Thanks for your help!

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Great! Thanks for posting back with your success.

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

Similar Threads

  1. Replies: 4
    Last Post: 07-27-2011, 09:52 AM
  2. Replies: 4
    Last Post: 05-11-2011, 03:06 AM
  3. Pass Variable to Table Field with VBA
    By dgj32784 in forum Programming
    Replies: 3
    Last Post: 03-28-2011, 09:36 PM
  4. Initialize field with a blank
    By GregFarrell in forum Forms
    Replies: 5
    Last Post: 02-16-2011, 01:50 PM
  5. Replies: 3
    Last Post: 05-06-2010, 03:26 PM

Tags for this Thread

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