Shadowshiroy
New Member
- Joined
- Jul 18, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Good Morning,
I have been working on a VBA code in order to modify data in a MariaDB database through a UserForm in Excel. The first time I run it works perfectly, the problem arises when I save it and try to open it again. It gives me a message of corrupted file and erases the VBA code. I think the problem is allocated in the Module code, when I define the macro to show the UserForm through a button displayed in Excel as just saving the UserForm does not arise any problem.
I attach the codes I have been using:
I have been working on a VBA code in order to modify data in a MariaDB database through a UserForm in Excel. The first time I run it works perfectly, the problem arises when I save it and try to open it again. It gives me a message of corrupted file and erases the VBA code. I think the problem is allocated in the Module code, when I define the macro to show the UserForm through a button displayed in Excel as just saving the UserForm does not arise any problem.
I attach the codes I have been using:
VBA Code:
**MODULE CODE**
Sub Enterdata()
UserForm1.Show
End Sub
**USERFORM CODE**
*ADD DATA*
Private Sub CommandButton1_Click()
Dim valor As String
Dim rowAffected As Integer
valor = TextBox1.Value
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
Sql = "INSERT INTO tipusprojecte(TipusProjecte) values('" & valor & "');"
con.Execute Sql, rowAffected
If rowAffected = 1 Then
MsgBox "Añadido"
Else
MsgBox "Fail"
End If
con.Close
End Sub
*DELETE DATA*
Private Sub CommandButton2_Click()
Dim valor As String
Dim rowAffected As Integer
valor = TextBox1.Value
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
Sql = "DELETE FROM tipusprojecte WHERE TipusProjecte = '" & valor & "';"
con.Execute Sql, rowAffected
If rowAffected = 1 Then
MsgBox "Eliminado"
Else
MsgBox "Fail"
End If
con.Close
End Sub
*UPDATE DATA*
Private Sub CommandButton3_Click()
Dim valor As String
Dim rowAffected As Integer
valor = TextBox1.Value
valorini = TextBox2.Value
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
Sql = "UPDATE tipusprojecte SET TipusProjecte = '" & valor & "' WHERE TipusProjecte = '" & valorini & "';"
con.Execute Sql, rowAffected
If rowAffected = 1 Then
MsgBox "Actualizado"
Else
MsgBox "Fail"
End If
con.Close
End Sub
*RESET ID*
Private Sub CommandButton4_Click()
Dim valor As String
Dim id As Integer
Dim rowAffected As Integer
valor = TextBox1.Value
valorini = TextBox2.Value
id = CInt(TextBox3.Value)
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.Open "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=XX;PORT=XX;DATABASE=XX;UID=XX;PWD=XX;"
Sql = "ALTER TABLE tipusprojecte AUTO_INCREMENT = " & id & ";"
con.Execute Sql, rowAffected
If rowAffected = 1 Then
MsgBox "Reset"
Else
MsgBox "Fail"
End If
con.Close
End Sub