Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Null column(0) on combo box via Code 1

Status
Not open for further replies.

Fattire

Technical User
Nov 30, 2006
127
US
I'm trying to give the user the ability to update a combo box to null if they no longer want a value from the combo box on the main table.

Code:
varSalespersonID = IIf(IsNull(Me.cboSalesPerson.Column(0)) = True, "", Me.cboSalesPerson.Column(0))

It's the INSERT statement that doesn't like the two single quotes next to themselves ('') as a value for the combo box id on the main table, the text boxes except the two single quotes just fine.

Any way around this?
 
Why not simply use:
Code:
  varSalespersonID = Me.cboSalesPerson.Column(0)
I don't have a clue what you mean by "INSERT statement ..." There is no insert statement in your code.


Duane
Hook'D on Access
MS Access MVP
 

Because that was giving a runtime 94, "Invalid use of Null" error before it even got to the insert sql:

Here's basically what it looks like, minus 15 other fields:

Code:
"INSERT INTO TBL_CUST (CUSTOMER_ID, SALESREP_ID) VALUES ('" & varCustID & "'," & varSalespersonID & ")

When I make the value "[]" on the IIF function and runs it through an UPDATE statement it works just fine, it takes the null - but not in an INSERT sql statement for a new record with no cbo value assigned.

So this works on an UPDATE
Code:
varSalespersonID = IIf(IsNull(Me.cboSalesPerson.Column(0)) = True, "[]", Me.cboSalesPerson.Column(0))

But the "[]" doesn't work in the INSERT.
 
I think you need to use the word Null with code like:
Code:
If IsNull(Me.cboSalesPerson.Column(0)) Then
   varSalesPersonID = " Null "
  Else
   'assume SALESREP_ID is text
   varSalesPersonID = """" & Me.cboSalesPerson.Column(0) & """"
End If
' ... "INSERT INTO TBL_CUST (CUSTOMER_ID, SALESREP_ID) VALUES (" & varCustID & "," & varSalespersonID & ")

Duane
Hook'D on Access
MS Access MVP
 
Nice job Duane...

Here's what it ended up being - works for both updates and inserts.

Code:
    If IsNull(Me.cboSalesPerson.Column(0)) Then
        varSalespersonID = " Null "
        Else
        varSalespersonID = """" & Me.cboSalesPerson.Column(0) & """"
    End If

    If IsNull(Me.cboCollector.Column(0)) Then
        varCollectorID = " Null "
        Else
        varCollectorID = """" & Me.cboCollector.Column(0) & """"
    End If

    If IsNull(Me.cboTerms.Column(0)) Then
        varTermsID = " Null "
        Else
        varTermsID = """" & Me.cboTerms.Column(0) & """"
    End If

    If IsNull(Me.txtCreditLimit) Then
        varCreditLimit = " Null "
        Else
        varCreditLimit = 0
    End If

Values ended up looking like this:

Code:
VALUES (454454,454,454,'HOOKA BAR 3','','','','', Null , Null , Null ,'','','','','','',0,False,False)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top