Why is it entering the loop? vb net mysql - mysql

I m new to vb net
So here is my problem
My database has 1 table=quiz and 3 columns of ID ,name and Flag
All flags are set to '0'
Whenever I delete ,instead of deleting ,I update the name field to xxx and Flag to 1
In my database ,ID =3 has name = xxx
Still y is it entering the loop? and printing the msg"OH no"?
my code :
Dim SQL as String
SQL = "SELECT Name FROM quiz WHERE Flag='0' AND ID='3'"
c = 0
If SQL <> "xxx"Then
' Try, Catch, Finally
i = i + 1
MsgBox("Ohh no")
END IF

Related

Insert only enabled textboxes into MySQL in VB.Net

I am creating an inventory management system in VB.Net, where the basic function is the process incoming invoices. I would like to insert data into my remote MySQL database but only if the textbox is considered enabled. I have no issue inserting data into the database when I want it to insert all fields. But I would like to have checkboxes enable certain products and allow the employees to enter only specific items. The check boxes do enable and disable the text fields as required but when inserting data into the database it will enter null values for all product types and I don't want it doing that as it will mess up the invoicing system. Currently I tried an if then statement but the issue I ran into was that it wanted the not enabled textboxes to be defined.
Code for what I tried is:
Public Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Dim mysqlconn as new MySqlConnection ("ServerConnection")
mysqlconn.Open()
dim mysqlCmd as new MysqlCommand
mysqlcmd.Connection = MysqlConn
mysqlcmd.CommandText = "Insert into Table_Name (Column1,Column2,Column3) Values (#rec1,#Rec2,#Rec3)"
If txtTextbox1.Enabled = True then
mysqlcmd.Parameters.AddWithValue("#Rec1",Column1.text)
End If
If txtTextBox2.Enabled = True then
mysqlcmd.Parameters.AddWithValue(#Rec2,Column2.text)
End IF
IF txtTextBox3.Enabled = True then
mysqlcmd.Parameters.AddWithValue(#Rec3,Column3.text)
End If
You can't just insert a column without a row. So if you will insert one column, you must insert all columns. If you don't want to add a value, then the column could be NULL or empty string, depending on the columns in the database. Also use Using and take the database work off the UI with Async/Await
Public Async Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
' if your columns take NULL to mean no value
Await AddParams(
If(txtTextbox1.Enabled, Column1.Text, Nothing),
If(txtTextbox2.Enabled, Column2.Text, Nothing),
If(txtTextbox3.Enabled, Column3.Text, Nothing))
' else, if an empty string means no value
Await AddParams(
If(txtTextbox1.Enabled, Column1.Text, ""),
If(txtTextbox2.Enabled, Column2.Text, ""),
If(txtTextbox3.Enabled, Column3.Text, ""))
End Sub
Private Function AddParams(param1 As String, param2 As String, param3 As String) As Task
Return Task.Run(
Sub()
Using mysqlconn As New MySqlConnection("ServerConnection")
mysqlconn.Open()
Using mysqlCmd As New MySqlCommand("Insert into Table_Name (Column1,Column2,Column3) Values (#rec1,#Rec2,#Rec3)", mysqlconn)
mysqlCmd.Parameters.AddWithValue("#Rec1", param1)
mysqlCmd.Parameters.AddWithValue("#Rec2", param2)
mysqlCmd.Parameters.AddWithValue("#Rec3", param3)
End Using
End Using
End Sub)
End Function
If I understand your current design,
Table_Name
ID
Column1
Column2
Column3
1
abc
def
ghi
2
jkl
mno
pqr
An incomplete invoice has empty string or NULL when one column isn't enabled
ID
Column1
Column2
Column3
1
abc
NULL
ghi
2
NULL
mno
pqr
This is what this answer does.
If your database design is flexible, perhaps a better design would be
Invoice
ID
Name
1
Invoice 1
2
Invoice 2
InvoiceRec
ID
Name
1
Column1
2
Column2
3
Column3
InvoiceItem
ID
InvoiceID
InvoiceRecID
Value
1
1
1
abc
2
1
3
ghi
3
2
2
mno
4
2
3
pqr
Now you aren't storing any null when an item is not enabled. The SQL to select would then be
SELECT
i.Name InvoiceName
, ir.Name InvoiceRecName
, ii.Value Value
FROM InvoiceItem ii
INNER JOIN Invoice i ON i.ID = ii.InvoiceID
INNER JOIN InvoiceRec ir ON i.ID = ir.InvoiceRecID
WHERE i.Name = 'Invoice 1'
InvoiceName
InvoiceRecName
Value
Invoice 1
Column1
abc
Invoice 1
Column3
ghi
This would totally invalidate the code I wrote (except keep the Async and Using) and you'd need to query multiple tables before making multiple inserts, but the design would be normalized and database storage size would be reduced.
Further, you can build your UI from the metadata tables [Invoice] and [InvoiceRec] so if wanted to add another InvoiceRec to your business, you can add in SQL, and the UI will not need to be modified.

VBA - MySQL UPDATE - Return code to confirm Update was done or not

Trying to run a MySQL update in Excel.
Would like to check if the UPDATE happened or not, i.e. was the row found and updated or such a row does not exist and 0 rows were updated
Tried to find a return code from MySQL in the VBA code but nothing I am at a loss!
c = 2
Do While Trim(MAIN.Cells(c, 1)) <> ""
sMyword = "UPDATE table1 SET price = 1.00 WHERE part = 'ABC';"
conn.Execute sMyword
If "did the update happen against the row" > 0 Then
MAIN.Cells(c, 3) = "Price UPDATED OK"
Else
MAIN.Cells(c, 3) = "Part NOT FOUND "
MAIN.Cells(c, 3).Interior.ColorIndex = 3 'Red
End If
c = c + 1
Loop
I wish to get the number of rows updated by the SQL statement sMyword
The Execute method should return the affected records in a second argument RecordsAffected. Try this syntax:
Dim RecordsAffected As Long
sMyword = "UPDATE table1 SET price = 1.00 WHERE part = 'ABC';"
conn.Execute sMyword, RecordsAffected
MsgBox RecordsAffected
reference: https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/execute-method-ado-connection?view=sql-server-2017

Return Unique Records for Field

We have an access database create a csv list in an access table for each userid that logs into a computer. The problem is that if a userid logs in multiple times on one computer, than the userid is duplicated.
SO let's say this is what the table structure looks like
computer DailyUsers
ABC-123 ml12, rs12, ml12, ml12, ee13
DEF-456 zx44, aa33, zx44
And this is what I want a query to return
Computer DailyUsers
ABC-123 ml12, rs12, ee13
DEF-456 zx44, aa33
I tried using both Group By and Distinct but that looks on a row by row basis, not a field basis
How can this be achieved in Access 2013?
You can create a custom function in VBA to return only the unique users.
Add this in a VBA module:
Option Compare Database
Option Explicit
Public Function DistinctOnly(txt As String)
Dim arr, v, rv As String
Dim d As Object
arr = Split(txt, ",")
If UBound(arr) > 0 Then
Set d = CreateObject("scripting.dictionary")
For Each v In arr
d(Trim(v)) = 1
Next v
rv = Join(d.keys, ",")
Else
rv = txt
End If
DistinctOnly = rv
End Function
Usage:
SELECT Table1.[ID], Table1.[Users], DistinctOnly(Table1.[Users]) as UsersX
FROM Table1;
Input data and query results:

Executing an update statement with a select subquery clause in C

I have the following sql that I run in C:
snprintf(sql, 200, "update rec set name = (select name from pers where id = %d )
where id = %d",rec_id , emp_id );
mysql_query(conn, sql) returns a successful result but it's putting 1 in the "rec" table in the "name" field instead of the name, but when I printf the output and use it in MySQL it's working fine.
update rec set name = (select name from pers where id = 104 ) where id = 43
Is there something wrong with my sprintf? Or something has to be added?
I also tried static sql command like this
snprintf(sql,"update rec set name = (select name from pers where id = 104 ) where id = 43");
and it also put 1 in the rec.name
Is that due to count of record returned by the sub query? Can you verify by putting a condition which returns e.g. 2 records so that the name is set to 2? if this is the reason then (though less performing approach) try splitting the queries and see if it works this time.

how to get the value of a column in recordset with space in the name?

we have change the server of our client, i'm trying to get rid of all the errors i got in the new server, i've got this situation:
yesterday, on the old server this works fine:
sql = "SELECT * FROM tableName INNER JOIN tableName2 ON tableName.Code = tableName2.[Global dimension 1 code] WHERE [No_]="&request("aNumber")
set rs = conn.execute(sql)
if not rs.EOF then
subCat = rs("Global dimension 1 code")
else
subCat = 0
end if
today, on the new server with IIS6 and same sql server 2008 as the oldone, i get the variable "subCat" empty, BUT if i write:
subCat = rs("code")
it goes good. Also:
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br>")
next
Response.Write("<br>")
rs.MoveNext
loop
gives all the columns, also those with spaces, and things like:
subCat = rs("[Global dimension 1 code]")
subCat = rs("'Global dimension 1 code'")
subCat = rs("[tableName].[Global dimension 1 code]")
rise a 500 server error.
anyone has a hint to point a column in recordset with spaces? i'm usign ASP classic.
thanks in advance.
Explicitly specify the fields to be selected :
Select fieldname1, fieldname2 from ...
instead of
Select * from ...