Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2011
    Posts
    56

    Unanswered: what am i doing wrong? (easy VBA script)

    im trying to get VBA to perform an action on the form that acts as a default value.. because im not sure how to actually use the default value facility..

    Code:
    Private Sub ItemBrand_AfterUpdate(Cancel As Integer)
    
    Dim Nullstr1 As String
    
    Nullstr1 = "No Brand"
    
        If Me.ItemBrand.Value = Null Then
        Me.ItemBrand.Value = Nullstr1
        
        End If
    End Sub
    I can't see what the bloody problem is?

    thanks

  2. #2
    Join Date
    Oct 2011
    Posts
    71
    Good Afternoon,

    If I understand my events correclty, you cannot update a control by using its own update control most of the time. The only time that event fires is when something has changed in the control. So if nothing is typed in there and its skipped over then it doesn't fire. However, if you have text in there and remove it all, it might fire then.

    For that type of coding i use the gotfocus and lostfocus events.

    Put that same code into the lost focus event and it will work every time.
    If you are using it on a form that can move through your records, then you'll have to put that code in the current event also. To ensure it updates as you scroll through the records.

    Hope it helps!

  3. #3
    Join Date
    Oct 2011
    Posts
    56
    i forgot to mention that i was a retard,

    i did what you said about the Lostfocus, and put them both in there, how do i implement the "Current" event prodecure?

    thanks

  4. #4
    Join Date
    Oct 2011
    Posts
    56
    heres what i tryed and still no results, any ideas buddy?



    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Form_Current()
    
    Dim Nullstr As String
    
    Nullstr = "No Number"
    
    If Me.ItemSerialNumber.Value = Null Then
        Me.ItemSerialNumber.Value = Nullstr
        
        End If
    
        
    Dim Nullstr1 As String
    
    Nullstr1 = "No Brand"
    
    If Me.ItemBrand.Value = Null Then
        Me.ItemBrand.Value = Nullstr1
        
        End If
    
        
    End Sub
    
    Private Sub Item_Description_NotInList(NewData As String, Response As Integer)
    
    Dim strTmp As String
    
    strTmp = "Add '" & NewData & "' as a new item?"
    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in List") = vbYes Then
    
        strTmp = "INSERT INTO ItemslistTbl ( Item ) " & _
            "SELECT """ & NewData & """ AS Item;"
            DBEngine(0)(0).Execute strTmp, dbFailOnError
            
            Response = acDataErrAdded
            
        End If
        End Sub
        
        
        
    
    
    Private Sub ItemBrand_LostFocus()
    
    Dim Nullstr1 As String
    
    Nullstr1 = "No Brand"
    
    If Me.ItemBrand.Value = Null Then
        Me.ItemBrand.Value = Nullstr1
        
        End If
    
        
    End Sub
    
    
    Private Sub ItemSerialNumber_LostFocus()
    
    Dim Nullstr As String
    
    Nullstr = "No Number"
    
    If Me.ItemSerialNumber.Value = Null Then
        Me.ItemSerialNumber.Value = Nullstr
        
        End If
    
    
    End Sub

  5. #5
    Join Date
    Oct 2011
    Posts
    71
    Hey its no problem we have all been there beofre.

    For On Current use the same code but instead of selecting a control make sure your form is selected and it should be your top event On Current.

    Hope it helps!

  6. #6
    Join Date
    Oct 2011
    Posts
    71
    Some quick things. Instead of Me.ItemSerialNumber.Value = Null try Me.ItemSerialNumber.Value & "" = ""

    I know it seems odd, but there is a difference between null and "" if you add "" to a null its becomes "" so that statement works for all empty strings you want to work with.

    Secondly your ItemSerialNumber is its text or numbers? If its numbers this isn't going to work, you'll have to change the control on the box and ghost "no brand" onto it.

    Hope it helps!

  7. #7
    Join Date
    Oct 2011
    Posts
    56
    fack it! im still being retarded, can you help me? like edit my script or sumthin? thankies

    Retarded jack :-)

  8. #8
    Join Date
    Oct 2011
    Posts
    71
    Private Sub Form_Current()

    If Me.ItemSerialNumber & "" = "" Then
    Me.ItemSerialNumber = "No Number"
    End If
    End Sub

    Assuming ItemSerialNumber will allow text. This should update any record you have that has an empty Me.ItemSerialNumber

    Once its in there start scrolling through your records with the navigation buttons or you own buttons and it should work.

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you cannot test a value for equality with null
    instead test if the variable is null
    it seem semantically picky but NULL means nothing, and you can't check if something = nothing.

    if isnull(myvar) = vbtrue then
    Me.ItemBrand.Value = "unbranded"
    endif

    incidentally this is VBA (Visual Basic for Applications) NOT VBScript although they are related they are no the same. VB Scripting is a visual basic dervied language used for automating interactions with the operating system
    I'd rather be riding on the Tiger 800 or the Norton

  10. #10
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Even better: The value of the textbox could be a zero-length string and not a Null value if the definition of the field to which it is bound does not allow Null values but allows zero-length string values. So I would test for:
    Code:
    If Nz(Me.ItemSerialNumber.Value, "") = "" Then
    That way, Null or zero-length string values would be tested with the same results.
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •