Using Split form to update data - ms-access

I have 2 tables, Tbl_Store1 & Tbl_Store2
I have one form (Frm_Data) that will populate from Tbl_Store1 or Tbl_Store2 based on an unbound text box called Txt_TblToUse.
Txt_TblToUse is a drop down field that only offers the user 2 choices. Store1 or store2.
After the user selects or changes Txt_TblsToUse I have the program set to update the data source to the correct table and then refresh, requery and repaint the form.
The frm_Data has a default view as a "Split Form"
The left side of the form is Form View
The Right side of the form is DataSheet View
The left side of the form (Form View) updates correctly
The right side of the form (DataSheet View) does not update to the new data.
If I right click on a header of one of the DataSheet view fields then chose to sort it, the data then re-populates with the correct data.
Is there a way to consistently update the data on the datasheet part of the form?
Code***
Call TableToUse 'This will look in the Module1 and determine which table to use
**TableToUse Code
Public Function TableToUse()
Form_Frm_Data.RecordSource = ""
Dim M_Month As Integer
M_Month = Form_Frm_Data.Txt_Month
Select Case Form_Frm_Data.Txt_TblToUse
Case Is = "Store1"
Form_Frm_Data.RecordSource = "Qry_Store1"
Form_Frm_Data.Requery
Case Is = "Tbl_Store2"
Form_Frm_Data.RecordSource = "Qry_Store2"
Form_Frm_Data.Requery
End Select
Dim db As String
db = ""
db = Form_Frm_Data.RecordSource
Form_Frm_Data.OrderBy = "TransactionDate ASC"
Form_Frm_Data.OrderBy = "TransactionDate DES"
Form_Frm_Data.Txt_DB = db
Form_Frm_Data.Recalc
Form_Frm_Data.Refresh
Form_Frm_Data.Repaint
Form_Frm_Data.ID.ColumnHidden = True
End Function

Related

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

My code is working, but I just want to know if there is a more efficient way to achieve the same effect.
I have this layout in a form:
In my effort to foolproof the record creation process, I would like to have the "Save and Clear fields" button enabled only after all but the 'Comment' textbox/combobox contains some valid data.
The text/combo boxes are called txtBatteryID, cmbModelNumber, cmbChemistryType, txtSpecVoltage, txtSpecCapacity.
My code is as follow
Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
If Me.btnSaveAndCLear.Enabled = False Then
If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
Me.btnSaveAndCLear.Enabled = True
End If
End If
End Sub
As you can see, I did the most straightforward way of using AND to combine all must-have conditions in an IF statement. This sub is called in After_Update() event of each text/combo box. Like this:
Private Sub cmbChemistryType_AfterUpdate()
Call EnableSaveBtnCheck
End Sub
My question, in the end, is: Is there a more efficient way to setup the condition "all text/combo box need to have something valid in them"? And is there a more elaborate way to check if the condition is met (something like a event on the form itself)?
Add the values of those 5 fields. If any of them is Null, the sum will be Null. So you only need call IsNull() once.
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

How to pass the values from accessed database to the new formed database?

My project is to create an emenu.For this I have created a database in phpmyadmin.The menu has been accessed by using code below.And now I want to send that menu item to a new database(that has been already created) on clicking the button "add to cart".I cannot seem to find a way to pass the value as a variable.i.e.name,price in this code. This is my code:
class Appetizers(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller=controller
def insdb():
cro=mysql.connector.connect(user='k***',password='***',host='******',database='newdb')
mycursor=cro.cursor()
mycursor.execute("""INSERT INTO order_frm_client(name,quantity,price) VALUES(name,2,price)""" )
cro.commit()
conn=mysql.connector.connect(user='k***',password='***',host='*****',database='menu')
mycursor=conn.cursor()
mycursor.execute("SELECT * FROM APPETIZERS")
results=mycursor.fetchall()
yo=50
for row in results:
id=row[0]
name=row[1]
price=row[3]
listBox = Listbox(self, height=1,width=40,highlightcolor="red")
listBox.place(x=100,y=yo)
listBox.insert(END,name)
listBox1 = Listbox(self, height=1,width=10,highlightcolor="red")
listBox1.place(x=350,y=yo)
listBox1.insert(END,price)
spin=Spinbox(self, from_=0, to=10, width=10).place(x=420,y=yo)
button=Button(self, text='Add to Cart',width=4, command=insdb(name,price))
button.place(x=500,y=yo)
yo=yo+30

MS Access: ComboBox Requery

I have a ComboBox which will display data which dependent on what has been entered in another ComboBox. The second ComboBox is not updating (showing) data in the dropdown after the first has been updated...
Company_Select_Combo is the first ComboBox. After a selection has been made in this combobox, Marketer_Select_Combo becomes visible on the form, and this query should reflect those marketers associated to the company selected in the first combo...
Help
Private Sub Company_Select_Combo_AfterUpdate()
If Company_Select_Combo > 0 Then
Company_Select_Combo.Enabled = False
Marketer_Select_Combo.Visible = True
Me.Marketer_Select_Combo.Requery
Else
Marketer_Select_Combo.Enabled = False
End If
End Sub
Let assume your "Marketer_Select_Combo" query includes:
Marketer_ID, Marketer_Name, and Company_ID
Then simply add the following statement to your SQL:
WHERE Company_ID = [Forms]![yourformname]![Company_Select_Combo]
This way, the elements should be limited to the chosen company. By calling the Requery/Recalc method as you did, it should work as intended.

VB.net Autocomplete Textbox filter using mysql database as custom source

I'm Having a problem regarding to the autocomplete textbox. First I already made the autocomplete textbox work with mysql database as custom source but the default textfilter of autocomplete is "start with" not "contains". I want to change the textfilter to "contains", so that when I search any part of the string, the whole name which contains the searched word will appear in the autocomplete suggestions.
Can anyone help me fix my code?
This is the code i've done so far:
txtSearch.AutoCompleteMode = AutoCompleteMode.SuggestAppend
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim DataCollection As New AutoCompleteStringCollection()
Dim query As String
sqlcon = New MySqlConnection
sqlcon.ConnectionString =
"server=localhost;userid=root;password=root;database=svfmemberlistdb"
Try
sqlcon.Open()
query = " SELECT Name FROM svfmemberlistdb.svfmemberlist "
sqlcmd = New MySqlCommand(query, sqlcon)
sqladr.SelectCommand = sqlcmd
sqladr.Fill(ds)
sqladr.Dispose()
sqlcon.Close()
For Each row As DataRow In ds.Tables(0).Rows
If row.ToString.Contains(txtSearch.Text) Then
DataCollection.Add(row(0).ToString())
End If
Next
Catch ex As Exception
End Try
txtSearch.AutoCompleteCustomSource = DataCollection
I quote here Mitja Bonca's answer on MSDN.
In this case, autocompletemode will just not do. Its code is not meant
for something like it.
You will have to do your own code, to do the filtering on each letter
press.
So I would suggest not to use autocompletemode, and get all the data
(names) into dataTable. When user presses some button ("1" for
example), you start with your filtering, by creating new Datatable
(leave the main one untached - so you can return back to all data when
clearing comboBox by backspace), with Copy() method - to create a full
copy of original one, and use Select method to do the filteing.
This should look something like by using % simbol on both sides of a
string - to filter inbetween - this is what you want!
DataTable AllNames = new DataTable();
//fill it up and leave it untouched!
//to filter comboBox with names that contains pressed characters do in
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string name = string.Format("{0}{1}", comboBox1.Text, e.KeyChar.ToString()); //join previous text and new pressed char
DataRow[] rows = table.Select(string.Format("FieldName LIKE '%{0}%'", name));
DataTable filteredTable = AllNames.Clone();
foreach(DataRow r in rows)
filteredTable.ImportRow(r);
comboBox1.DataSource = null;
comboBox1.DataSource = filteredTable.DefaultView;
comboBox1.DisplayMember = "FieldName";
}
Reference
EDIT: This is of course a c# answer not VB.NET but it might be helpful to get the concept.

Send message to user if they leave all fields blank when adding data to table; and don't add the data

So, I have a table, 2 forms, a sub form, 2 buttons, and a bunch of text boxes. With these I made a button that pops up an "add field" where you type information into text boxes then click add, and it adds it to the table which back on the other subform shows that data; I have that working. I am not sure how to make it not add the data if you leave ALL fields blank though (note; I only want it to be ALL fields, it's okay if they fill out one field and the rest are null). If all fields are null, it sends out a msgbox to the user saying please fill in data into the text boxes.
Another thing I am looking for for a future part of this database is required fields. Where lets say there are 10 text boxes, and before you can add the data into the table through the form you MUST fill out 6 out of 10 of the text boxes (marked with a * which ones are needed) and if you don't, give them an error saying please fill out [text boxes that weren't filled out] and try again. If all of the 6 required fields were filled out, then it can save it to the table even if the other 4 are null or not.
This is the code I have inside of the add button:
Private Sub CustomerAddBtn_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from CustomersT")
rec.AddNew
rec("CustomerName") = Me.CustomerAddSupplierNameTxt
rec("Address") = Me.CustomerAddAddressTxt
rec("City") = Me.CustomerAddCityTxt
rec("ProvinceState") = Me.CustomerAddProvinceStateTxt
rec("PostalZip") = Me.CustomerAddPostalZipTxt
rec("Phone") = Me.CustomerAddPhoneTxt
rec("Fax") = Me.CustomerAddFaxTxt
rec("CustomerSince") = Me.CustomerAddCustomerSinceTxt
rec("Email") = Me.CustomerAddEmailTxt
rec("Notes") = Me.CustomerAddNotesTxT
rec.Update
Set rec = Nothing
Set db = Nothing
'Send message to user saying it was saved, so they know
Dim intReply As Integer
intReply = MsgBox("Customer has been successfully saved to the database!", vbOKOnly, "Success!")
End Sub
Thanks in advance.
You can check if a Text Box has a value in it by using IsNull(Me.TextBoxName). So, if you want to test if all of the text boxes are empty then you can do something like this
If IsNull(Me.CustomerAddSupplierNameTxt) _
And IsNull(Me.CustomerAddAddressTxt) Then
MsgBox "Please don't try to enter an empty record."
Else
MsgBox "(user filled in at least one field)", vbInformation, "Debug Message"
' your existing database code here
End If
...and just expand the initial If statement to include all of the controls you want to check.
Similarly, for required fields, you can check the value of the controls corresponding to those required fields and display a similar message if any of them IsNull().