views:

52

answers:

3

For some sort of reason, which i can't seem to spot, this fail and i get my #Name? error, when i tried to do this, i use pretty much the same code another place in my form, the only difference is the cbo and the names of my txt and alike.

Private Sub cboVarenummer_AfterUpdate()
  Dim LSQLVareNavn  As String
    LSQLVareNavn = "select Varenavn from VARENUMMER where VARENUMMER.Varenummer = '" & cboVarenummer & "'"
    txtVarenavn.ControlSource = LSQLVareNavn
End Sub

Can anyone spot my error?

what i wish the code would do is when i Select something in my cboVarenummer, the my txtbox txtVarenavn changes its text to what the sql statement returns?

A: 

Did you not want cboVarenummer.SelectedValue.Tostring (Or cboVarenummer.SelectedItem.Tostring dependant on how you populated the combo box) in the SQL Query?

Ben
well im not the origanl creator, but the raw source is this: SELECT DISTINCTROW Varenummerf.Varenummer, Varenummerf.Varenavn, Varenummerf.Varenavn2 FROM Varenummerf; And it can't find the SelectedItem nor value, only something called SelItem...
Ilizane
I just hit mé This is bc i use access 2003, i Got both value and text, but non of Them seems to work? What am i doing wrong?
Ilizane
Ben
Hey ben, im act. not really sure what im using, its the lang that comes with access 2003, and i think .net is from 2000, but might be my memory that fails me ^^ anyway, i found another solution but thanks anyway :P
Ilizane
A: 

This is ms-access, right?

In which case, I do not believe you wish to set the Control Source (normally the name of a control in the bound table, a constant, or a function) to an SQL string, I believe you want the RowSource.

In addition, is Varenummer a text field? If not, drop the single quotes.

Remou
varenummer is a textfield, even though its a number act, but again, i was not the original creator and i found another way around it. but thanks anyway
Ilizane
A: 

what i wish the code would do is when i Select something in my cboVarenummer, the my txtbox txtVarenavn changes its text to what the sql statement returns

In that case, I think you should be changing txtVarenavn's .Value property rather than its .ControlSource property.

Also use the DLookup() function to retrieve the value you want to store in txtVarenavn.Value

Private Sub cboVarenummer_AfterUpdate()
    'Dim LSQLVareNavn  As String '
    Dim strWhere As String
    'LSQLVareNavn = "select Varenavn from VARENUMMER where VARENUMMER.Varenummer = '" & cboVarenummer & "'" '
    'txtVarenavn.ControlSource = LSQLVareNavn '
    strWhere = "Varenummer = '" & Me!cboVarenummer & "'"
    Me!txtVarenavn = DLookup("Varenummer", "Varenummerf", strWhere)
End Sub

Compare the DLookup expression to your previous SELECT statement. You can see how the field name, table name, and WHERE clause correspond between the two. Think of DLookup as a simple SELECT ... it returns only a single value rather than a recordset ... but that's exactly what you need here.

I also changed the table name to Varenummerf, as you listed in your comment to @Ben. VARENUMMER as the table and Varenummer as the field name might not confuse Access' database engine, but it makes me uneasy. :-)

HansUp
i think ill stick with DLookup :P it works and i get the result i want.. Thanks for the help :P
Ilizane