SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why my code is unable to check the textbox is empty or not? pls help

    I have crated a userform in access 2007 and used this code to insert in table.

    Private Sub Save_Click()
    If School.Value = "" Then
    MsgBox ("Enter School ID")
    School.SetFocus
    Exit Sub
    End If

    Dim insertstring As String

    DoCmd.SetWarnings (False)
    insertstring = "INSERT INTO Classroom (School_id) VALUES ('" & School.Value & "' );"
    DoCmd.RunSQL insertstring
    end sub

    When i press SAVE command for the first time with Schooltxtbox empty, the values are inserted into table without checking if the text box for school is empty. Again for the second time when i click save then it only the checks for the textbox if its empty. Why is this psl help...

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    Your if is setting the value to empty. It should be:
    if School.Value == ""

    But better yet, use the string helper class to check:
    String.IsNullOrEmpty(School.Value) this will return a boolean if it is empty / null or not

  3. #3
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if School.Value == ""
    This produce compile error while using ==

    My above code works well, but it doesn't check the text box in first press to command, it insert blank in the table in first and for next entry only it checks for the text box if it's empty or not.

  4. #4
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy THIS IS YET SIMPLE BUT DONT WORK WHY??? pls help Urgent.

    I have just a user form in access 2007 with a text box and a Save command.
    MY code:

    Private Sub Save_Click()
    If School.Value = "" Then
    MsgBox ("Text Box is empty")
    else
    Msgbox(" Not Empty")

    End If
    End Sub

    When the Save is clicked, and the text box is empty, the second statement is executed with message "Not Empty".
    Why this

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Agree with NightStalker-DNS on using String.IsNullOrEmpty, but I'd take it one step further and use String.IsNullOrWhiteSpace

    Try the following:
    Code VB.NET:
    Private Sub Save_Click()
        If String.IsNullOrWhiteSpace(School.Value) Then
            MsgBox ("Text Box is empty")
        else 
            Msgbox(" Not Empty")
        End If
    End Sub

    If that continues to fail, try the following:
    Code VB.NET:
    Private Sub Save_Click()
        If School.Value.Trim().Length = 0 Then
            MsgBox ("Text Box is empty")
        else 
            Msgbox(" Not Empty")
        End If
    End Sub

  6. #6
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Agree with NightStalker-DNS on using String.IsNullOrEmpty, but I'd take it one step further and use String.IsNullOrWhiteSpace

    Try the following:
    Code VB.NET:
    Private Sub Save_Click()
        If String.IsNullOrWhiteSpace(School.Value) Then
            MsgBox ("Text Box is empty")
        else 
            Msgbox(" Not Empty")
        End If
    End Sub

    If that continues to fail, try the following:
    Code VB.NET:
    Private Sub Save_Click()
        If School.Value.Trim().Length = 0 Then
            MsgBox ("Text Box is empty")
        else 
            Msgbox(" Not Empty")
        End If
    End Sub
    Thanks Cpradio, it worked well and Now i have another question :
    I have used the following code to delete record form table:
    DoCmd.RunSQL "DELETE * FROM Tbl1 WHERE Grade = '" & txtGrade.Value & "'" This works and
    what if i want the query to watch two values from the table like Grade and Year or even third value like School_Nmae and delete the one that matches all these values i put in textbox to delete.
    p.s Make it also for number values, how should the code be ( Colons like things)

    Thanks in advance.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Anuanusha View Post
    Now i have another question :
    I have used the following code to delete record form table:
    DoCmd.RunSQL "DELETE * FROM Tbl1 WHERE Grade = '" & txtGrade.Value & "'" This works and
    what if i want the query to watch two values from the table like Grade and Year or even third value like School_Nmae and delete the one that matches all these values i put in textbox to delete.
    p.s Make it also for number values, how should the code be ( Colons like things)

    Thanks in advance.
    Actually, I'm a bit surprised that even works, as I would have thought the * between DELETE and FROM would fail, however, if it works, it works. To expand it to only delete values that match on multiple columns, your syntax would be

    Code SQL:
    DoCmd.RunSQL "DELETE FROM Tbl1 WHERE Grade = '" & txtGrade.VALUE & "' AND Year = '" & txtYear.VALUE & "' AND School_Name = '" & School.VALUE & "'"

    However, I will say this, you have opened yourself to a SQL Injection with this process so be sure to sanitize your data (verify it is legit). Granted I see this is against an Access database, so your tools to do that, may be limited.

    I did a bit of quick searching and found you can use parameters when working with access if you change how you connect to access
    http://stackoverflow.com/questions/1...ry-using-regex (see last example on that page)

  8. #8
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot Cpradio, I'll get back to you if there comes any problems, yet i have not checked with this code. Thanks a ton.


Bookmarks

Posting Permissions

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