Unable to use function's Variable in a sub in MS-Access - ms-access

I have the below VBA code on my ms-access form
Option Compare Database
Option Explicit
Public Function GetUserName() As String
Dim obj1 As Object
Dim GetEmpID As String
Set obj1 = CreateObject("WScript.Network")
GetUserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
GetEmpID = DLookup("[EmpID]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
Set obj1 = Nothing
End Function
Private Sub Form_Load()
Me.EmpName.Value = GetUserName
Me.EmpID.Value = GetEmpID
End Sub
I am able to get the value using GetUserName in my Load() sub but unable to get the value using GetEmpID.
What can be the possible reason? And how can i fix it?
Thanks in advance.

Option 1: Create module level variables, which you set in the function (now a sub, since it is returning no value)
Option Compare Database
Option Explicit
Dim UserName as String
Dim EmpID as String
Public Sub GetValues()
Dim obj1 As Object
Set obj1 = CreateObject("WScript.Network")
UserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
EmpID = DLookup("[EmpID]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
Set obj1 = Nothing
End Sub
Private Sub Form_Load()
Call GetValues() 'Invoke the function to set values
Me.EmpName.Value = UserName 'retrieve values from module level variables
Me.EmpID.Value = EmpID
End Sub
Option 2: Create ByRef variables.
Option Compare Database
Option Explicit
Public Sub GetValues(ByRef UserName as String, ByRef EmpID as String)
Dim obj1 As Object
Set obj1 = CreateObject("WScript.Network")
UserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
EmpID = DLookup("[EmpID]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
Set obj1 = Nothing
End Sub
Private Sub Form_Load()
Dim UserName as String
Dim EmpID as String
Call GetValues(UserName, EmpId) 'Invoke the function to set values of local variables.
Me.EmpName.Value = UserName 'retrieve values from function scoped variables.
Me.EmpID.Value = EmpID
End Sub
Warning: typing from memory. minor corrections may be needed.

You could always use the ByRef in your function.
Option Compare Database
Option Explicit
Public Function GetUserName(ByRef GetEmpID as string) as string
dim obj1 as object
Set obj1 = CreateObject("WScript.Network")
GetUserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
GetEmpID = DLookup("[EmpID]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
If not obj1 is nothing then
Set obj1 = Nothing
End If
End Function
Private Sub Form_Load()
Dim GetEmpID as string
GetEmpID = "None"
Me.EmpName.value = GetUserName(GetEmpID)
Me.EmpID.Value = GetEmpID
End Sub
The ByRef passes the address that is used to store the variable in memory. Once the address is passed instead of just the value, the any changes that happen to the address will be made to the variable across the project.
Please see website below for more detailed description of ByVal and ByRef:
http://www.cpearson.com/excel/byrefbyval.aspx

Related

not able to update access table field using VBA

I have a userform in access which submits the data to a table, there are two fields in table which I want to get populated automatically whenever user makes a new entry in form. I am using the below vba code with that form :
Option Compare Database
Public Function GetUserName() As String
Dim obj1 As Object
Set obj1 = CreateObject("WScript.Network")
GetUserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & obj1.UserName & "'")
Set obj1 = Nothing
End Function
Private Sub Form_BeforeInsert(Cancel As Integer)
Owner2 = CreateObject("WScript.Network").UserName
AdvisorName = GetUserName
End Sub
Private Sub Form_Load()
Me.DataForm.Form.Recordset.MoveLast
End Sub
Private Sub SaveBtn_Click()
RunCommand acCmdSaveRecord
Me.Requery
Me.DataForm.Form.Recordset.MoveLast
End Sub
I am able to get the value in "owner" field but not in "AdvisorName" field.
What can be the possible reason behind this ?
is there any mistake in my code or is there any better method of doing this ?
The below code should commit both values to your table using a querydef. This is my preferred approach but there are other ways to do it as well. The qdf represents a sql string that can accept parameters from code. We are inserting the values of our variables directly into the sql string.
I copied the code from the save button to re-establish the form state after entry. This should bring the last record back up, but it will depend on the cursortype of the recordset whether the saved record is represented or not (it might go backwards).
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim qdf As QueryDef
Dim Owner2 As String
Dim AdvisorName As String
Owner2 = CreateObject("WScript.Network").UserName
AdvisorName = GetUserName
Set qdf = CurrentDb.CreateQueryDef("", "INSERT INTO Mau_con (Owner, AdvisorName) VALUES ('" & Owner2 & "', '" & AdvisorName & "')")
qdf.Execute
Me.Requery
Me.DataForm.Form.Recordset.MoveLast
Set qdf = Nothing
End Sub

Calling value inside a Userform's Control using a variable

I am trying to call a value from a form to put it on SQL. But it doesn't work.
Dim n As integer
Dim StrSQL As String
Dim mymain As Variant
Dim inputform as string
mymain = Array("well_name", "well_alias", "well_short")
n=0
--try to loop
inputform="form_inputwell"+mymain(n)
StrSQL = "INSERT INTO databasewell (bh_name) VALUES ('" & inputform & "');"
DoCmd.RunSQL StrSQL
n=n+1
--loop end
Any idea to call the value which field inside an array?
for example :
form_inputwell.well_name called by declaring "form_inputwell"+mymain(1)
I found my solution, but thanks anyway for helping.
paramtemp = Forms("form name").Controls(field in form).Value
I think that is what you are trying to do :
Dim n As Integer
Dim StrSQL As String
Dim mymain As Variant
Dim inputform As String
mymain = Array("well_name", "well_alias", "well_short")
For n = LBound(mymain) To UBound(mymain)
inputform = form_inputwell.Controls(mymain(n)).Value
StrSQL = "INSERT INTO databasewell (bh_name) VALUES ('" & inputform & "');"
DoCmd.RunSQL StrSQL
Next n

Access Form's textbox not showing value

I have an unbound textbox in form f_FeuilleBleue. In my code, I give it a certain value;
Debug.Print strAHNS '00 0AA 00-100 F TX-01
Form_f_FeuilleBleue.txt_AHNS = strAHNS
If I put a stopping point on the next line, the immediate window shows that
?Form_f_FeuilleBleue.txt_AHNS
answer: 00 0AA 00-100 F TX-01
However, I still see it as blank in my form. There is no data to be read!
How do I fix this? Is it an issue with screen updating? (I have nothing setting it to off) Maybe form updating? (I have a msgBox in the BeforeUpdate event but it doesn't go in that event)
EDIT - additional info:
When I open the form, there is no problem. I can change the value in the form or by code. However the issue only happens when the form is opened from a menu-style form. Code below. Even after the opening sub finishes, the textbox won't update (visually - it does in value). After testing I see that the Change and the Update event are NOT launched when changing the value of the textbox from another sub (Private subs may be the cause?) But why is it continuing to not show the values even after the subs end?
Could be very, very relevant to read but I'm not sure what to make of it: Obtaining textbox value in change event handler
Here is the code that opens the form:
Private Sub Command7_Click()
Dim strAHNS As String
Dim strquery As String
strAHNS = Replace(Mid(Me.Combo_Dessin2, InStr(Me.Combo_Dessin2, "=") + 1), "=", " ")
strquery = "[ID] = (SELECT Max([ID]) FROM [Feuilles])"
Debug.Print strquery
If (PremierAffichage) Then
DoCmd.OpenForm FormName:="f_feuillebleue", WhereCondition:=strquery
Else
MsgBox "Le projet ou dessin n'a pas été trouvé."
End If
End Sub
Function PremierAffichage() As Boolean
Dim rsFeuilles As DAO.Recordset
Dim rsProjets As DAO.Recordset
Dim strContrat As String
Dim strProjet As String
Dim strDessin As String
Dim sqlquery As String
Dim strAHNS As String
Dim strGroupe As String
Dim strMachine As String
If IsNull(Me.Combo_Dessin2) Or IsNull(Me.Combo_Projet) Or Me.Combo_Dessin2.Value = "" Then
PremierAffichage = False
Exit Function
End If
strProjet = Me.Combo_Projet
strAHNS = Me.Combo_Dessin2
strMachine = Mid(strAHNS, 4, 3)
strGroupe = Mid(strAHNS, 8, 2)
Debug.Print strProjet & " ** " & strAHNS & " ** " & strMachine & " ** " & strGroupe
sqlquery = "SELECT [AHNS], [Contrat], [No Projet], [EP (groupe)], [Type machine], [Mois] FROM [Feuilles]" 'WHERE [AHNS] = '" & strAHNS & "'"
Set rsFeuilles = CurrentDb.OpenRecordset(sqlquery)
sqlquery = "SELECT [Projet HNA] FROM [Projets] WHERE [Projet AHNS] = '" & strProjet & "'"
Set rsProjets = CurrentDb.OpenRecordset(sqlquery)
Debug.Print strAHNS '========================================--------
Form_f_FeuilleBleue.txt_AHNS = strAHNS ' this works in .value but not showing the result
DoEvents ' any changes from there on don't update the value visually
' ==========================================------
If rsProjets.RecordCount > 0 Then
rsFeuilles.AddNew
rsFeuilles![Contrat] = rsProjets![Projet HNA]
rsFeuilles![No Projet] = strProjet
rsFeuilles![AHNS] = strAHNS
rsFeuilles![Mois] = MonthName(Mid(Date, 6, 2))
If strMachine Like "[A-Z][A-Z][A-Z]" Then
rsFeuilles![Type machine] = strMachine
rsFeuilles![EP (groupe)] = strGroupe
End If
rsFeuilles.Update
PremierAffichage = True
End If
rsProjets.Close
Set rsProjets = Nothing
rsFeuilles.Close
Set rsFeuilles = Nothing
End Function
Assign your value to the active form instance (the open form) instead of to the form's class.
So assuming the open form's name is f_FeuilleBleue and you want to assign that value to the form's txt_AHNS control ...
'Form_f_FeuilleBleue.txt_AHNS = strAHNS
Forms!f_FeuilleBleue!txt_AHNS = strAHNS
Reference the form by its name as a member of the Forms collection.

Multiselect listbox in Access 2010

So I have this search form that filters projects based on the designer type. The designer can be internal, external, or combination. It was originally done using a combobox. I was asked to make it so that the user can filter by multiple options, so I changed it to a listbox with the idea of using the multiselect option.
The listbox works fine, until I set the multiselct option to anything besides none. Then, all the projects are returned instead of the ones that are selected.
I'm not very experienced in Access or VBA so any assistance would be greatly appreciated. The following is the code for this section.
Private Sub toDesignerExcel_Click()
Dim db As DAO.Database
Dim rs1, rs2 As DAO.Recordset
Dim sSQL1, sSQL2, SourceExcel, FileName, Path As String
Set db = CurrentDb
sSQL1 = "Select ExcelPath from tblBackendFiles where Setting = 'ExcelDesignerParameters'"
sSQL2 = "Select Setting from tblBackendFiles where Code = 'SourceExcel'"
Set rs1 = db.OpenRecordset(sSQL1)
FileName = Nz(rs1!ExcelPath, "")
Set rs2 = db.OpenRecordset(sSQL2)
SourceExcel = Nz(rs2!Setting, "")
Path = SourceExcel + "\" + FileName
rs1.Close
rs2.Close
db.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
Shell "C:\WINDOWS\explorer.exe """ & Path, vbNormalFocus
End Sub
EDIT:
And also,
If Forms(formName).txtDesigner <> "" And Not IsNull(Forms(formName).txtDesigner) Then
If selEngConditions <> "" Then
selEngConditions = selEngConditions & " AND "
End If
selEngConditions = selEngConditions & "[Activity].[GWPDesigner] = '" & Forms(formName).txtDesigner & "'"
End If
I would do something like this :
Private Sub toDesignerExcel_Click()
Dim db As DAO.Database
Dim rs1, rs2 As DAO.Recordset
Dim sSQL1, sSQL2, SourceExcel, FileName, Path As String
Set db = CurrentDb
sSQL1 = "Select ExcelPath from tblBackendFiles where Setting = 'ExcelDesignerParameters' And [Tblbackendfiles] = '"
sSQL2 = "Select Setting from tblBackendFiles where Code = 'SourceExcel' And [Tblbackendfiles] = '"
For Each it In Me.ListBoxName.ItemsSelected
Set rs1 = db.OpenRecordset(sSQL1 & it & "'")
FileName = Nz(rs1!ExcelPath, "")
Set rs2 = db.OpenRecordset(sSQL2 & it & "'")
SourceExcel = Nz(rs2!Setting, "")
Path = SourceExcel + "\" + FileName
Shell "C:\WINDOWS\explorer.exe """ & Path, vbNormalFocus
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Next it
db.Close
Set db = Nothing
End Sub
Where you basically iterate over all the selected items in the listbox. This adds each selected item from the listbox in the where clause of the query.
You can also try concatenating all the values and changing the query to something like :
And [Tblbackendfiles] In (" & comma_separated_list & ")

error when update data ms access using visual basic 2010

**i have simple application, but i dont know how to fix it.
this pic when i try to edit my database--> http://i861.photobucket.com/albums/ab171/gopak/sa_zps5a950df5.jpg
when i click button edit i want my access data will be update.this is my code..
thanks for your advice**
Imports System.Data.OleDb
Public Class Form2
Public cnstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gop's\Downloads\admin site\admin site\admin site\bin\Debug\data_ruangan.accdb"""
Public cn As New OleDbConnection
Public cmd As New OleDbCommand
Public adaptor As New OleDbDataAdapter
Private Sub logout_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles logout_btn.Click
Form1.Show()
Me.Close()
End Sub
Private Sub exit_btn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exit_btn.Click
Dim a As Integer
a = MsgBox("Are you sure want to exit application?", vbInformation + vbYesNo, "Admin Site Virtual Tour Application")
If a = vbYes Then
End
Else
Me.Show()
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Data_ruanganDataSet.data_ruangan' table. You can move, or remove it, as needed.
Me.Data_ruanganTableAdapter.Fill(Me.Data_ruanganDataSet.data_ruangan)
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i = DataGridView1.CurrentRow.Index
Label7.Text = DataGridView1.Item(0, i).Value
txtName.Text = DataGridView1.Item(1, i).Value
txtLocation.Text = DataGridView1.Item(2, i).Value
txtCapacity.Text = (DataGridView1.Item(3, i).Value).ToString
txtOperational.Text = (DataGridView1.Item(4, i).Value).ToString
txtInformation.Text = DataGridView1.Item(5, i).Value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'If txtName.Text <> "" And txtLocation.Text <> "" And txtCapacity.Text <> "" And txtOperational.Text <> "" And txtInformation.Text <> "" Then
Dim i = DataGridView1.CurrentRow.Index
Dim ID = DataGridView1.Item(0, i).Value
Dim cmd As New OleDb.OleDbCommand
If Not cn.State = ConnectionState.Open Then
cn.Open()
End If
cmd.Connection = cn
cmd.CommandText = ("update data_ruangan set Name = '" & txtName.Text & _
"',Location = '" & txtLocation.Text & "',Capacity = '" & txtCapacity.Text & _
"',Operational_Hours ='" & txtOperational.Text & "',Information = '" & txtInformation.Text & ";")
cmd.ExecuteNonQuery()
cn.Close()
txtName.Text = ""
txtLocation.Text = ""
txtCapacity.Text = ""
txtOperational.Text = ""
txtInformation.Text = ""
'End If
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Try to use a parameterized query to execute your code. The error message is relative to the fact that you haven't initialized your connection with the information contained in the ConnectionString.
Here an example on how to do that...... but......
Dim cmdText = "update data_ruangan set [Name] = ?,Location = ?,Capacity = ?, " & _
"Operational_Hours =?,Information = ?;"
Using cn = new OleDbConnection( cnstring )
Using cmd = OleDb.OleDbCommand(cmdText, cn)
cn.Open
cmd.Parameters.AddWithValue("#p1", txtName.Text)
cmd.Parameters.AddWithValue("#p2", txtLocation.Text)
cmd.Parameters.AddWithValue("#p3", txtCapacity.Text)
cmd.Parameters.AddWithValue("#p4", txtOperational.Text)
cmd.Parameters.AddWithValue("#p5", txtInformation.Text)
'''' WARNING ''''
' WITHOUT A WHERE STATEMENT YOUR QUERY WILL UPDATE
' THE WHOLE TABLE WITH THE SAME VALUES
'''' WARNING ''''
cmd.ExecuteNonQuery()
End Using
End Using
txtName.Text = ""
txtLocation.Text = ""
txtCapacity.Text = ""
txtOperational.Text = ""
txtInformation.Text = ""
This code updates all the records of your table with the same value, so, unless you have only one record and update Always the same record you need to add a WHERE condition to your command.
Also, the NAME word is a reserved keyword in Access 2007/2010 and it is better to encapsulate that word with square brackets to avoid a syntax error message.
I have also removed the global variable OleDbConnection and used a local one that will be closed and destroyed when the code exits from the Using statement. This is the correct way to handle disposable objects, in particular every connection object is Always to be used in this way to release as soon as possible the expensive unmanaged resource used by the object.