I am making a front-end application in VB. The back-end is Oracle. I want an autogenerated ID on the click of a "New" button. It works well if data is present in the table, but shows an error if the table is empty. What do I need to insert so that it works when I am using the application for the first time? My button code is as follows:
Private Sub cmd_new_Click()
Call txt_clear
txt_name.Enabled = True
Set rsCat = New ADODB.Recordset
rsCat.Open "Category", conn, adOpenDynamic, adLockPessimistic
If rsCat.EOF = rscat.BOF Then
tempId = 1000
Else
rsCat.MoveLast
tempId = rsCat.Fields("Category_Id") + 1
End If
txt_Id = tempId
cmd_Save.Enabled = True
cmd_new = False
End Sub
check rscat.RecordCount=-1
Basically, change
If rsCat.EOF = rscat.BOF Then
to
If rsCat.RecordCount=-1 Then
Related
I have created a form to allow a user to change their password.
I created a recordset and used edit/update to save it in a query, but the new password is not being saved in the query.
My code is as follows:
Private Sub txtNewPass2_AfterUpdate()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("qryUsers", dbOpenDynaset)
If Me.txtNewPass1 = Me.txtNewPass2 Then
rst.MoveFirst
Do Until rst.EOF
If rst!NName = CboUserName.Column(0) Then
rst.Edit
rst!Password = txtNewPass2.Value
rst.Update
End If
rst.MoveNext
Loop
DoCmd.Openform("frmLogin")
Else: MsgBox "Passwords not Matching"
End If
End Sub
The expression:
DoCmd.Openform("frmLogin")
Will result in a syntax error, as the parentheses surrounding the arguments are not required when the value returned by the function is not used.
However, you may find it cleaner to simply execute a SQL statement to perform the update, rather than iterating over the recordset, i.e.:
Private Sub txtNewPass2_AfterUpdate()
If txtNewPass1 = txtNewPass2 Then
With CurrentDb.CreateQueryDef("", "UPDATE qryUsers SET qryUsers.Password = ?pwd WHERE qryUsers.NName = ?usr")
.Parameters(0) = txtNewPass2
.Parameters(1) = CboUserName.Column(0)
.Execute
End With
DoCmd.Openform "frmLogin"
Else
MsgBox "Passwords not Matching"
End If
End Sub
Using parameters of course to account for users better known as Bobby Tables.
lets call it modUser.
In this modUser I have a ADODB record set
Now from this modUser, I would like to open a form. Lets call it frmUser2.
when this frmUser2 initialize. I would like to use the recordset that I already have from modUser. How do I pass this recordset from modUser to frmUser2?
I tried creating a public sub under frmUser2. But I get an error that says "Run time error 13 type mismatch"
here is a snippet
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount > 1 Then
frmUser2.PopulateList(rs)
End if
in frmUser2 I have the public function ( i tried sub too)
Public Function PopulateList(rs As ADODB.Recordset)
For Count = 0 To rs.RecordCount - 1
LstModels.AddItem rs(0)
rs.MoveNext
Next
rs.close
End Function
I tried show , and I can get the form to appear, but I have no way to pass the record set.
frmUser2.Show
Please help. Thank you
I'm not a fan of how you're trying to do this, but working with what you want to do, first create a public Recordset property in your form and assign the recordset to it from your module before showing the form.
Module code:
Dim objForm As frmUser2
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount.EOF = False Then
Set objForm = New frmUser2
frmUser2.Recordset = rs
frmUser2.Show
End if
Form Code:
Private Sub Form_Load()
If Not Recordset Is Nothing Then
PopulateList
End If
End Sub
Public Function PopulateList()
Recordset.MoveFirst 'defensive, make sure we're on the first record
LstModels.Clear
Do While Recordset.EOF = False
LstModels.AddItem Recordset(0)
Recordset.MoveNext
Next
Recordset.Close
End Function
I think it would be preferable for the module to have a public method that returns an employee recordset. Your form would call that method when it needs the data. Set rsEmployees = modUser.GetEmployees()
I have this query below and I want to show items that sold depending on sales_id without repeat the old ids. And another question how to make a combo box data grid view:
Private Sub filldatagrid()
Dim myTableName As String
myTableName = txtid.Text
winclass.filldatagrid(DataGridView1, "select inventory.inv_name,sales_data.sales_id,sales_data.quantity,sales_data.sum_total from sales_data,inventory where sales_data.inv_id=inventory.inv_id ")
DataGridView1.Columns(0).HeaderText = "ID"
DataGridView1.Columns(0).Visible = True
DataGridView1.Columns(1).HeaderText = "name"
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End Sub
I have this form on VS2012 with asp.net. First I do search for the patron, then go to verify information for that patron. This patron information is displayed in ItemTemplate(ReadOnly). If that is not the patron they are looking for then they can add a new patron with "New button" (asp.net code). I am able to get the id of the new Patron(which is PK). However I am not able to display this newly created record on the form after inserting. It still displays the record which was on display. Since this a formview I did not enable "Paging".
Is it possible to call the pageload event from datasource_inserted event? Then I can pass the new patron ID for display. I declared this ID as global variable?
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim lvPatronID As String
lvPatronID = Request.QueryString("value1")
If lvPatronID = "" Then
frmPatronView.ChangeMode(FormViewMode.Insert)
Else
frmPatronView.ChangeMode(FormViewMode.ReadOnly)
GvPatronID = lvPatronID
lblPatronID.Text = GvPatronID
End If
Protected Sub PatronDS_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles PatronDS.Inserted
NewID = e.Command.Parameters("#PatronID").Value.ToString
GvPatronID = NewID
End Sub
Well I answered part of my own question. The following change to the Inserted event will let me view the newly inserted data. I have another button to add new record in the emptytemplate of the search form. This is why I am changing the mode to insert as the default mode is readonly. This will let me insert the data but after inserting it doesn't display the form at all. Not sure why Inserted event is not kicking in properly.
Protected Sub PatronDS_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles PatronDS.Inserted
Dim NewID As String = Nothing
Try
NewID = e.Command.Parameters("#PatronID").Value.ToString
PatronDS.SelectCommand = "SELECT * FROM tblPatron WHERE PatronID='" & NewID & "'"
lblPatronID.Text = NewID.Trim()
frmPatronView.DataBind()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub txtUserCode_Validate(Cancel As Boolean)
If RS!ID = txtUserCode.Text Then
SQL = "SELECT NAME,PRIVILEDGE FROM ADMIN WHERE CODE=" & txtUserCode.Text
Set RS = CN.Execute(SQL)
txtUserName.Text = RS!NAME
Else
MsgBox "ENTER VALID NO"
txtUserCode.Text = ""
Cancel = True
End If
End Sub
In this code I want to execute like:
If I enter the ID present in table then it'll show info but it's considering 1st record (RS!ID(0)) only not the next one
If I enter the ID which is not present in table then it should not throw error
3021- Requested operation requires current record but goto else part.
Please Help
I am assuming RS is a recordset.
Depending on the RS type, you can try and Find a record like this:
RS.MoveFirst
RS.Find("[CODE]=" & txtUserCode.Text)
If Not RS.EOF Then
' found!
End If
Link to the ADO Find function.