I wrote the below code in VB.net to check if ID not exist then to add record in database, the code is not working with condition and add the record without verify if the record already exist or not.
Please advise
rs.MoveFirst()
Do While Not rs.EOF
If rs("ID").Value <> TextBox1.Text Then
rs.AddNew()
rs("CustomerName").Value = TextBox2.Text
rs("CustomerAddress").Value = TextBox3.Text
rs.Update()
End If
rs.MoveNext()
Loop
Related
Can someone explain what I'm doing wrong here.
The goal is simple: when checking a checkbox, then a certain record needs to be updated:
here's my code:
Set PASOFT = CurrentDb
sql = "SELECT * FROM <TABLE> WHERE WERKORDER='" & [WO_NUMMER].Value & "'"
Set rs = CurrentDb.OpenRecordset(sql)
Dim sWo As String
With rs
If Not .BOF And Not .EOF Then
'Ensure that the recordset contains records
'If no records the code inside the if...end if
'statement won't run
.MoveLast
.MoveFirst
'Not necessary but good practice
If .Updatable Then
'It is possible that the record you want to update
'is locked by another user. If we don't check before
'updating, we will generate an error
.Edit
'Must start an update with the edit statement
.fields("KOSTEN_KOMPLEET") = 1
'Another way of accessing the fields would be to use
'.fields("FirstName") = z" & .fields("FirstName")
.Update
'And finally we will need to confirm the update
End If
End If
.Close
'Make sure you close the recordset...
End With
The sql which is executed is this: "SELECT * FROM <table> WHERE WERKORDER='22103326.01'"
All seems fine to me. When I open the table manually and look for the value I find one row.
Screenshot of Table
Your help is greatly appreciated
I am new in vb.net. i have a database and i am running a search query of employee records when search button clicked and display that information in textbox, however when a user is not in the database, the search output is displaying the information of the previous search. the information in textbox should display blank or say "No record found" if the user is not in the record. not sure what is wrong in my code.
Try
myConnection.Open()
Dim str As String
str = "SELECT * FROM tblEmp WHERE (EmpID = '" & ADS.UserEmpID & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read
If dr.HasRows > 0 Then
MessageBox.Show("user already in the system", "Warning", MessageBoxButtons.OK)
ElseIf dr.HasRows = 0 Then
MessageBox.Show("Not Onboarded", "Warning", MessageBoxButtons.OK)
End If
BGC1 = dr("PreStartChecks").ToString
BGC2 = dr("EmpName").ToString
myConnection.Close()
End While
Catch ex As Exception
MsgBox("Unable to Connect to BGC DB. You may not have access or DB not available." &
ex.ToString)
End Try
Your While loop and If statement don't make sense. Firstly, HasRows is type Boolean so testing whether it is greater than zero is nonsensical. Secondly, Read returns False if there are no rows so the only way you can get to that If statement is if there is at least one row to read so testing HasRows when you already know that there are rows is also nonsensical. The proper option here is to use just an If statement and test Read only:
If dr.Read() Then
'There is a row to read and it was just read, so you can now get the data from the reader.
Else
'There is no row to read.
End If
If you want to clear a control when there's no data, you do so in the Else block.
The "rules" about when and how to use HasRows and Read are very simple and logical:
If all you care about is whether the query result set contains data or not but you don't care what that data is, just use an If statement to test HasRows. The HasRows property is type Boolean so there's no need to compare it to anything. It already is True or False.
If there can only be zero or one row in the result set, just use an If statement to call Read and test the result. Again, it's type Boolean so there's no need to compare it to anything. If it returns True then you can access the data for the row you just read.
If there can be multiple rows and you don't want to do anything special if there are no rows then just use a While or Do While loop to call Read and access the row that was just read inside the loop.
If there can be multiple rows and you do want to do something special if there are no rows, use an If statement to test HasRows and then a While or Do While loop inside the If block to call Read. You would handle the case where there are no rows in the Else block.
Assuming that txtBGC1 and txtBGC2 are TextBoxes, you could do something like this, assuming that the query can at most return one employee
...
If dr.Read Then ' There is an employee
txtBGC1.Text = dr("PreStartChecks").ToString
txtBGC2.Text = dr("EmpName").ToString
Else ' There is no employee
txtBGC1.Text = ""
txtBGC2.Text = "No record found"
End If
myConnection.Close()
I have one button in user interface which I click once will update/edit all records in column/field Action containing the value 'Not yet Acted' to be filled with value 'Acted' and all blank records/rows in column/field DateRecieved will be filled with todays date. The code below is working and no error but it update/edit only the first row/record and the following rows/records are not edit or updated. Please help me.
atskoneksyon()
atsrec = New ADODB.Recordset
With atsrec
.Open("Select * from ATS where Action='" & "Not yet Acted" & "'", atscon, 2, 3)
.Fields("DateReceived").Value = Format(Now, "MM/dd/yyyy")
.Fields("Action").Value = "Acted"
MsgBox("Updated")
.Update()
End With
atscon.Close()
You need to loop over each record you wish to update the records.
With atsrec
.Open("Select * from ATS where Action='" & "Not yet Acted" & "'", atscon, 2, 3)
While Not .EOF
.Fields("DateReceived").Value = Format(Now, "MM/dd/yyyy")
.Fields("Action").Value = "Acted"
.Update()
.MoveNext()
End While
End With
or you can run the following update query which will be faster as it doesn't need to retrieve the records.
UPDATE ATS SET DateReceived=Now, Action='Acted'" WHERE Action='Not yet Acted'
I have an access database that my team uses to track projects. We use it as a VCB.
On the main form (Projects) we view it and make updates on the form for the projects - which in turn updates the table linked to that form.
However, we also have a separate table (Comments) where we store comments related to the projects. The Primary Key for both tables is ProjectID.
There is only 1 Project entry on the Projects form but there can be 0 to Many on Comments on the comment table.
What I am trying to achieve is have the Project Form (our VCB) look at the comments table and see if there is a record in the table with the same ProjectID - if there is insert a "Yes" value into my textbox on the project form but if there is not then insert "NO".
Its just an easy visual aid to see if comments exist for the projects on the VCB (we handle comment entry and viewing in a separate form)
I am stumped on which route to take. Ive tried a few things and gotten stuck in loops. Any help is appreciated
Dim strSQL As String
Dim rs As Dao.Recordset
Dim db As Dao.Database
strSQL = "SELECT * FROM COMMENTS WHERE [PROJECTID] = " & Me.PROJECTID & ""
Set db = CurrentDb
Set rs = db.OpenRecordset("COMMENTS")
rs.MoveFirst
Do Until rs.EOF = True
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Me.CommentTxtBox.SetFocus
Me.CommentTxtBox.Text = "NO"
Else
Me.CommentTxtBox.SetFocus
Me.CommentTxtBox.Text = "YES"
End If
rs.MoveLast
Loop
rs.Close
Set rs = Nothing
I'm getting every entry a NO in the field and it errors out because the ProjectID is not in the comments table(which it might not be) so it says no record found.
DCount() could make your code simpler.
Dim lngComments As Long
Dim strComments As String
lngComments = DCount("*", "COMMENTS", "[PROJECTID] = " & Me.PROJECTID)
If lngComments > 0 Then
strComments = "YES"
Else
strComments = "NO"
End If
Me.CommentTxtBox.Value = strComments
Notice by assigning the value to the text box's .Value property (instead of its .Text property), you don't have to bother about SetFocus.
I think that is a simpler version of your current approach. However I'm uncertain whether that is the best approach. Perhaps you could get what you need by setting the text box's Control Source property to an expression which uses IIf to evaluate DCount:
=IIf(DCount("*", "COMMENTS", "[PROJECTID] = " & [PROJECTID]) > 0, "YES", "NO")
Using VB.Net and Sql Server
I want to check the user Entry Value.
The User is entering the code in the textbox, before saving to the table, i want to check whethere code is already exist in the table or not.
Tried Code
cmd = New SqlCommand("Select code from table where code = '" & textbox1.Text & "' ", Con)
dr = cmd.ExecuteReader()
While dr.Read()
End While
If value is exist, then message to the user "Already Exist" other wise save to the table.
Need Vb.net Code Help
Use SELECT COUNT instead and then check for that being greater than zero:
cmd = New SqlCommand("SELECT COUNT(*) from table where code = '" & textbox1.Text & "' ", con)
Dim NumRecords as Int32 = cmd.ExecuteScalar
IF NumRecords > 0 THEN...
Make code as primary key since you want it to be unique
if the value entered by the user in code is already existing in the table , VIOLATION of primary key sql exception will be thrown
Catch that exception and display a warning message!